Debian Codegraph @

tpm2/test/nv_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "testing"
8
9 . "github.com/google/go-tpm/tpm2"
10 "github.com/google/go-tpm/tpm2/transport/simulator"
11 )
12
13 func TestNVAuthWrite(t *testing.T) {
14 thetpm, err := simulator.OpenSimulator()
15 if err != nil {
16 t.Fatalf("could not connect to TPM simulator: %v", err)
17 }
18 defer thetpm.Close()
19
20 def := NVDefineSpace{
21 AuthHandle: TPMRHOwner,
22 Auth: TPM2BAuth{
23 Buffer: []byte("p@ssw0rd"),
24 },
25 PublicInfo: New2B(
26 TPMSNVPublic{
27 NVIndex: TPMHandle(0x0180000F),
28 NameAlg: TPMAlgSHA256,
29 Attributes: TPMANV{
30 OwnerWrite: true,
31 OwnerRead: true,
32 AuthWrite: true,
33 AuthRead: true,
34 NT: TPMNTOrdinary,
35 NoDA: true,
36 },
37 DataSize: 4,
38 }),
39 }
40 if _, err := def.Execute(thetpm); err != nil {
41 t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
42 }
43
44 pub, err := def.PublicInfo.Contents()
45 if err != nil {
46 t.Fatalf("%v", err)
47 }
48 nvName, err := NVName(pub)
49 if err != nil {
50 t.Fatalf("Calculating name of NV index: %v", err)
51 }
52
53 prewrite := NVWrite{
54 AuthHandle: AuthHandle{
55 Handle: pub.NVIndex,
56 Name: *nvName,
57 Auth: PasswordAuth([]byte("p@ssw0rd")),
58 },
59 NVIndex: NamedHandle{
60 Handle: pub.NVIndex,
61 Name: *nvName,
62 },
63 Data: TPM2BMaxNVBuffer{
64 Buffer: []byte{0x01, 0x02, 0x03, 0x04},
65 },
66 Offset: 0,
67 }
68 if _, err := prewrite.Execute(thetpm); err != nil {
69 t.Errorf("Calling TPM2_NV_Write: %v", err)
70 }
71
72 read := NVReadPublic{
73 NVIndex: pub.NVIndex,
74 }
75 readRsp, err := read.Execute(thetpm)
76 if err != nil {
77 t.Fatalf("Calling TPM2_NV_ReadPublic: %v", err)
78 }
79 t.Logf("Name: %x", readRsp.NVName.Buffer)
80
81 write := NVWrite{
82 AuthHandle: AuthHandle{
83 Handle: TPMRHOwner,
84 Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
85 },
86 NVIndex: NamedHandle{
87 Handle: pub.NVIndex,
88 Name: readRsp.NVName,
89 },
90 Data: TPM2BMaxNVBuffer{
91 Buffer: []byte{0x01, 0x02, 0x03, 0x04},
92 },
93 Offset: 0,
94 }
95 if _, err := write.Execute(thetpm); err != nil {
96 t.Errorf("Calling TPM2_NV_Write: %v", err)
97 }
98 }
99
100 func TestNVAuthIncrement(t *testing.T) {
101 thetpm, err := simulator.OpenSimulator()
102 if err != nil {
103 t.Fatalf("could not connect to TPM simulator: %v", err)
104 }
105 defer thetpm.Close()
106
107 // Define the counter space
108 def := NVDefineSpace{
109 AuthHandle: TPMRHOwner,
110 Auth: TPM2BAuth{
111 Buffer: []byte("p@ssw0rd"),
112 },
113 PublicInfo: New2B(
114 TPMSNVPublic{
115 NVIndex: TPMHandle(0x0180000F),
116 NameAlg: TPMAlgSHA256,
117 Attributes: TPMANV{
118 OwnerWrite: true,
119 OwnerRead: true,
120 AuthWrite: true,
121 AuthRead: true,
122 NT: TPMNTCounter,
123 NoDA: true,
124 },
125 DataSize: 8,
126 }),
127 }
128 if _, err := def.Execute(thetpm); err != nil {
129 t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
130 }
131
132 pub, err := def.PublicInfo.Contents()
133 if err != nil {
134 t.Fatalf("%v", err)
135 }
136 // Calculate the Name of the index as of its creation
137 // (i.e., without NV_WRITTEN set).
138 nvName, err := NVName(pub)
139 if err != nil {
140 t.Fatalf("Calculating name of NV index: %v", err)
141 }
142
143 incr := NVIncrement{
144 AuthHandle: AuthHandle{
145 Handle: TPMRHOwner,
146 Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
147 },
148 NVIndex: NamedHandle{
149 Handle: pub.NVIndex,
150 Name: *nvName,
151 },
152 }
153 if _, err := incr.Execute(thetpm); err != nil {
154 t.Errorf("Calling TPM2_NV_Increment: %v", err)
155 }
156
157 // The NV index's Name has changed. Ask the TPM for it.
158 readPub := NVReadPublic{
159 NVIndex: pub.NVIndex,
160 }
161 readPubRsp, err := readPub.Execute(thetpm)
162 if err != nil {
163 t.Fatalf("Calling TPM2_NV_ReadPublic: %v", err)
164 }
165 incr.NVIndex = NamedHandle{
166 Handle: pub.NVIndex,
167 Name: readPubRsp.NVName,
168 }
169
170 read := NVRead{
171 AuthHandle: AuthHandle{
172 Handle: TPMRHOwner,
173 Auth: HMAC(TPMAlgSHA256, 16, Auth([]byte{})),
174 },
175 NVIndex: NamedHandle{
176 Handle: pub.NVIndex,
177 Name: readPubRsp.NVName,
178 },
179 Size: 8,
180 }
181 readRsp, err := read.Execute(thetpm)
182 if err != nil {
183 t.Fatalf("Calling TPM2_NV_Read: %v", err)
184 }
185
186 if _, err := incr.Execute(thetpm); err != nil {
187 t.Errorf("Calling TPM2_NV_Increment: %v", err)
188 }
189
190 var val1 uint64
191 err = binary.Read(bytes.NewReader(readRsp.Data.Buffer), binary.BigEndian, &val1)
192 if err != nil {
193 t.Fatalf("Parsing counter: %v", err)
194 }
195
196 readRsp, err = read.Execute(thetpm)
197 if err != nil {
198 t.Fatalf("Calling TPM2_NV_Read: %v", err)
199 }
200
201 var val2 uint64
202 err = binary.Read(bytes.NewReader(readRsp.Data.Buffer), binary.BigEndian, &val2)
203 if err != nil {
204 t.Fatalf("Parsing counter: %v", err)
205 }
206
207 if val2 != (val1 + 1) {
208 t.Errorf("want %v got %v", val1+1, val2)
209 }
210 }
211
212 func TestNVWriteLock(t *testing.T) {
213 thetpm, err := simulator.OpenSimulator()
214 if err != nil {
215 t.Fatalf("could not connect to TPM simulator: %v", err)
216 }
217 defer thetpm.Close()
218
219 // Define the NV space with attributes that allow it to be locked
220 def := NVDefineSpace{
221 AuthHandle: TPMRHOwner,
222 PublicInfo: New2B(
223 TPMSNVPublic{
224 NVIndex: TPMHandle(0x0180000F),
225 NameAlg: TPMAlgSHA256,
226 Attributes: TPMANV{
227 OwnerWrite: true,
228 OwnerRead: true,
229 WriteSTClear: true, // Allow TPM2_NV_WriteLock to lock this index
230 },
231 DataSize: 4,
232 }),
233 }
234 if _, err := def.Execute(thetpm); err != nil {
235 t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
236 }
237
238 pub, err := def.PublicInfo.Contents()
239 if err != nil {
240 t.Fatalf("%v", err)
241 }
242 nvName, err := NVName(pub)
243 if err != nil {
244 t.Fatalf("Calculating name of NV index: %v", err)
245 }
246
247 // Write data to the NV space
248 write := NVWrite{
249 AuthHandle: AuthHandle{
250 Handle: TPMRHOwner,
251 Auth: PasswordAuth(nil),
252 },
253 NVIndex: NamedHandle{
254 Handle: pub.NVIndex,
255 Name: *nvName,
256 },
257 Data: TPM2BMaxNVBuffer{
258 Buffer: []byte{0x01, 0x02, 0x03, 0x04},
259 },
260 Offset: 0,
261 }
262 if _, err := write.Execute(thetpm); err != nil {
263 t.Errorf("Calling TPM2_NV_Write: %v", err)
264 }
265
266 // Lock the NV space against further writes
267 lock := NVWriteLock{
268 AuthHandle: AuthHandle{
269 Handle: TPMRHOwner,
270 Auth: PasswordAuth(nil),
271 },
272 NVIndex: NamedHandle{
273 Handle: pub.NVIndex,
274 Name: *nvName,
275 },
276 }
277 if _, err := lock.Execute(thetpm); err != nil {
278 t.Errorf("Calling TPM2_NV_WriteLock: %v", err)
279 }
280
281 // Try to write to the NV space again, which should fail because it's locked
282 write2 := NVWrite{
283 AuthHandle: AuthHandle{
284 Handle: TPMRHOwner,
285 Auth: PasswordAuth(nil),
286 },
287 NVIndex: NamedHandle{
288 Handle: pub.NVIndex,
289 Name: *nvName,
290 },
291 Data: TPM2BMaxNVBuffer{
292 Buffer: []byte{0x05, 0x06, 0x07, 0x08},
293 },
294 Offset: 0,
295 }
296 _, err = write2.Execute(thetpm)
297 if !errors.Is(err, TPMRCNVLocked) {
298 t.Errorf("TPM2_NV_Write succeeded after NV_WriteLock, expected it to fail")
299 }
300
301 // Verify we can still read the data
302 read := NVRead{
303 AuthHandle: AuthHandle{
304 Handle: TPMRHOwner,
305 Auth: PasswordAuth(nil),
306 },
307 NVIndex: NamedHandle{
308 Handle: pub.NVIndex,
309 Name: *nvName,
310 },
311 Size: 4,
312 Offset: 0,
313 }
314 readRsp, err := read.Execute(thetpm)
315 if err != nil {
316 t.Fatalf("Calling TPM2_NV_Read: %v", err)
317 }
318
319 // Verify the data is still the original data
320 expectedData := []byte{0x01, 0x02, 0x03, 0x04}
321 if !bytes.Equal(readRsp.Data.Buffer, expectedData) {
322 t.Errorf("Read data doesn't match expected data. Got %v, want %v", readRsp.Data.Buffer, expectedData)
323 }
324 }
325
326 func TestNVReadLock(t *testing.T) {
327 thetpm, err := simulator.OpenSimulator()
328 if err != nil {
329 t.Fatalf("could not connect to TPM simulator: %v", err)
330 }
331 defer thetpm.Close()
332
333 // Define the NV space with attributes that allow it to be locked for reading
334 def := NVDefineSpace{
335 AuthHandle: TPMRHOwner,
336 PublicInfo: New2B(
337 TPMSNVPublic{
338 NVIndex: TPMHandle(0x0180000F),
339 NameAlg: TPMAlgSHA256,
340 Attributes: TPMANV{
341 OwnerWrite: true,
342 OwnerRead: true,
343 ReadSTClear: true, // Allow TPM2_NV_ReadLock to lock this index
344 },
345 DataSize: 4,
346 }),
347 }
348 if _, err := def.Execute(thetpm); err != nil {
349 t.Fatalf("Calling TPM2_NV_DefineSpace: %v", err)
350 }
351
352 pub, err := def.PublicInfo.Contents()
353 if err != nil {
354 t.Fatalf("%v", err)
355 }
356 nvName, err := NVName(pub)
357 if err != nil {
358 t.Fatalf("Calculating name of NV index: %v", err)
359 }
360
361 // Write data to the NV space
362 write := NVWrite{
363 AuthHandle: AuthHandle{
364 Handle: TPMRHOwner,
365 Auth: PasswordAuth(nil),
366 },
367 NVIndex: NamedHandle{
368 Handle: pub.NVIndex,
369 Name: *nvName,
370 },
371 Data: TPM2BMaxNVBuffer{
372 Buffer: []byte{0x01, 0x02, 0x03, 0x04},
373 },
374 Offset: 0,
375 }
376 if _, err := write.Execute(thetpm); err != nil {
377 t.Errorf("Calling TPM2_NV_Write: %v", err)
378 }
379
380 // Read the data to verify it's accessible
381 read := NVRead{
382 AuthHandle: AuthHandle{
383 Handle: TPMRHOwner,
384 Auth: PasswordAuth(nil),
385 },
386 NVIndex: NamedHandle{
387 Handle: pub.NVIndex,
388 Name: *nvName,
389 },
390 Size: 4,
391 Offset: 0,
392 }
393 readRsp, err := read.Execute(thetpm)
394 if err != nil {
395 t.Fatalf("Calling TPM2_NV_Read before locking: %v", err)
396 }
397
398 // Verify the data is correct
399 expectedData := []byte{0x01, 0x02, 0x03, 0x04}
400 if !bytes.Equal(readRsp.Data.Buffer, expectedData) {
401 t.Errorf("Read data doesn't match expected data. Got %v, want %v", readRsp.Data.Buffer, expectedData)
402 }
403
404 // Lock the NV space against further reads
405 lock := NVReadLock{
406 AuthHandle: AuthHandle{
407 Handle: TPMRHOwner,
408 Auth: PasswordAuth(nil),
409 },
410 NVIndex: NamedHandle{
411 Handle: pub.NVIndex,
412 Name: *nvName,
413 },
414 }
415 if _, err := lock.Execute(thetpm); err != nil {
416 t.Errorf("Calling TPM2_NV_ReadLock: %v", err)
417 }
418
419 // Try to read from the NV space again, which should fail because it's locked
420 read2 := NVRead{
421 AuthHandle: AuthHandle{
422 Handle: TPMRHOwner,
423 Auth: PasswordAuth(nil),
424 },
425 NVIndex: NamedHandle{
426 Handle: pub.NVIndex,
427 Name: *nvName,
428 },
429 Size: 4,
430 Offset: 0,
431 }
432 _, err = read2.Execute(thetpm)
433 if !errors.Is(err, TPMRCNVLocked) {
434 t.Errorf("TPM2_NV_Read succeeded after NV_ReadLock, expected it to fail")
435 }
436 }