forked from canonical/secboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcr_profile_test.go
415 lines (403 loc) · 16.2 KB
/
pcr_profile_test.go
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/canonical/go-tpm2"
. "github.com/snapcore/secboot"
"github.com/snapcore/secboot/internal/testutil"
)
func TestPCRProtectionProfile(t *testing.T) {
for _, data := range []struct {
desc string
alg tpm2.HashAlgorithmId
profile *PCRProtectionProfile
pcrs tpm2.PCRSelectionList
values []tpm2.PCRValues
}{
{
// Verify that AddPCRValues works as expected
desc: "AddValues/1",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
},
},
{
// Verify that AddPCRValues overwrites previous values
desc: "AddValues/2",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
},
},
{
// Verify that (A1 || A2) && (B1 || B2) produces 4 outcomes
desc: "OR/1",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddProfileOR(
NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")),
NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo2"))).
AddProfileOR(
NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")),
NewPCRProtectionProfile().AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar2")))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo2"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar2"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo2"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar2"),
},
},
},
},
{
// Verify that (A1 && B1) || (A2 && B2) produces 2 outcomes
desc: "OR/2",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().AddProfileOR(
NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")),
NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo2")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar2")))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo2"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar2"),
},
},
},
},
{
// Verify that ExtendPCR without an initial value works as expected
desc: "Extend",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event1")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event2")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event3")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event4"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "event1", "event3"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "event2", "event4"),
},
},
},
},
{
// Verify that ExtendPCR after AddPCRValue works as expected
desc: "AddAndExtend",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event1")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event2"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo", "event1"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar", "event2"),
},
},
},
},
{
// Verify that ExtendPCR inside ProfileOR with initial PCR values works as expected
desc: "OR/3",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")).
AddProfileOR(
NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event1")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event2")),
NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event3")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event4")))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo", "event1"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar", "event2"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo", "event3"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar", "event4"),
},
},
},
},
{
// Verify that AddPCRValue inside ProfileOR with initial PCR values works as expected
desc: "OR/4",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")).
AddProfileOR(
NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event1")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")),
NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 12, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "event4")))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo", "event1"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
},
},
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
12: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar", "event4"),
},
},
},
},
{
// Verify that other PCR algorithms work
desc: "SHA1PCRs",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA1, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA1, "bar"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA1: {
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA1, "bar"),
},
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
},
},
},
},
{
// Verify that other PCR digest algorithms work
desc: "SHA1",
alg: tpm2.HashAlgorithmSHA1,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
},
},
{
// Verify that (A1 && B1) || (A1 && B1) is de-duplicated
desc: "DeDuplicate",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().AddProfileOR(
NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")),
NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar")))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
},
},
{
desc: "EmptyProfileOR",
alg: tpm2.HashAlgorithmSHA256,
profile: func() *PCRProtectionProfile {
return NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo")).
AddProfileOR().
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"))
}(),
values: []tpm2.PCRValues{
{
tpm2.HashAlgorithmSHA256: {
7: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "foo"),
8: testutil.MakePCRValueFromEvents(tpm2.HashAlgorithmSHA256, "bar"),
},
},
},
},
} {
t.Run(data.desc, func(t *testing.T) {
expectedPcrs := data.values[0].SelectionList()
var expectedDigests tpm2.DigestList
for _, v := range data.values {
d, _ := tpm2.ComputePCRDigest(data.alg, expectedPcrs, v)
expectedDigests = append(expectedDigests, d)
}
pcrs, pcrDigests, err := data.profile.ComputePCRDigests(nil, data.alg)
if err != nil {
t.Fatalf("ComputePCRDigests failed: %v", err)
}
if !pcrs.Equal(expectedPcrs) {
t.Errorf("Unexpected PCRSelectionList")
}
if !reflect.DeepEqual(pcrDigests, expectedDigests) {
t.Errorf("ComputePCRDigests returned unexpected digests")
t.Logf("Profile:\n%s", data.profile)
t.Logf("Values:\n%s", testutil.FormatPCRValuesFromPCRProtectionProfile(data.profile, nil))
}
})
}
}
func TestPCRProtectionProfileString(t *testing.T) {
profile := NewPCRProtectionProfile().
AddPCRValue(tpm2.HashAlgorithmSHA256, 7, make([]byte, tpm2.HashAlgorithmSHA256.Size())).
AddPCRValue(tpm2.HashAlgorithmSHA256, 8, make([]byte, tpm2.HashAlgorithmSHA256.Size())).
AddProfileOR(
NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "foo1")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "bar1")),
NewPCRProtectionProfile().
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "bar1")).
ExtendPCR(tpm2.HashAlgorithmSHA256, 8, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "foo1"))).
ExtendPCR(tpm2.HashAlgorithmSHA256, 7, testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "end"))
expectedTpl := `
AddPCRValue(TPM_ALG_SHA256, 7, %[1]x)
AddPCRValue(TPM_ALG_SHA256, 8, %[1]x)
AddProfileOR(
Branch 0 {
ExtendPCR(TPM_ALG_SHA256, 7, %[2]x)
ExtendPCR(TPM_ALG_SHA256, 8, %[3]x)
}
Branch 1 {
ExtendPCR(TPM_ALG_SHA256, 7, %[3]x)
ExtendPCR(TPM_ALG_SHA256, 8, %[2]x)
}
)
ExtendPCR(TPM_ALG_SHA256, 7, %[4]x)
`
expected := fmt.Sprintf(expectedTpl,
make([]byte, tpm2.HashAlgorithmSHA256.Size()),
testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "foo1"),
testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "bar1"),
testutil.MakePCREventDigest(tpm2.HashAlgorithmSHA256, "end"))
if expected != profile.String() {
t.Errorf("Unexpected string:\ngot:%s\nexpected:%s", profile, expected)
}
}
func TestPCRProtectionProfileAddValueFromTPM(t *testing.T) {
tpm, _ := openTPMSimulatorForTesting(t)
defer closeTPM(t, tpm)
if _, err := tpm.PCREvent(tpm.PCRHandleContext(7), []byte("foo"), nil); err != nil {
t.Fatalf("PCREvent failed: %v", err)
}
_, tpmValues, err := tpm.PCRRead(tpm2.PCRSelectionList{{Hash: tpm2.HashAlgorithmSHA256, Select: []int{7}}})
if err != nil {
t.Fatalf("PCRRead failed: %v", err)
}
p := NewPCRProtectionProfile().AddPCRValueFromTPM(tpm2.HashAlgorithmSHA256, 7)
pcrs, digests, err := p.ComputePCRDigests(tpm.TPMContext, tpm2.HashAlgorithmSHA256)
if err != nil {
t.Fatalf("ComputePCRDigests failed: %v", err)
}
if !pcrs.Equal(tpm2.PCRSelectionList{{Hash: tpm2.HashAlgorithmSHA256, Select: []int{7}}}) {
t.Errorf("ComputePCRDigests returned the wrong selection")
}
if len(digests) != 1 {
t.Fatalf("ComputePCRDigests returned the wrong number of digests")
}
expectedDigest, _ := tpm2.ComputePCRDigest(tpm2.HashAlgorithmSHA256, tpm2.PCRSelectionList{{Hash: tpm2.HashAlgorithmSHA256, Select: []int{7}}}, tpmValues)
if !bytes.Equal(digests[0], expectedDigest) {
t.Errorf("ComputePCRDigests returned unexpected values")
}
}