Debian Codegraph @

tpm2/test/sealing_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "errors"
6 "testing"
7
8 . "github.com/google/go-tpm/tpm2"
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 // Test creating and unsealing a sealed data blob with a password and HMAC.
13 func TestUnseal(t *testing.T) {
14 templates := map[string]TPMTPublic{
15 "RSA": RSASRKTemplate,
16 "ECC": ECCSRKTemplate,
17 }
18
19 // Run the whole test for each of RSA and ECC SRKs.
20 for name, srkTemplate := range templates {
21 t.Run(name, func(t *testing.T) {
22 unsealingTest(t, srkTemplate)
23 })
24 }
25 }
26
27 func unsealingTest(t *testing.T, srkTemplate TPMTPublic) {
28 thetpm, err := simulator.OpenSimulator()
29 if err != nil {
30 t.Fatalf("could not connect to TPM simulator: %v", err)
31 }
32 defer thetpm.Close()
33
34 // Create the SRK
35 // Put a password on the SRK to test more of the flows.
36 srkAuth := []byte("mySRK")
37 createSRKCmd := CreatePrimary{
38 PrimaryHandle: TPMRHOwner,
39 InSensitive: TPM2BSensitiveCreate{
40 Sensitive: &TPMSSensitiveCreate{
41 UserAuth: TPM2BAuth{
42 Buffer: srkAuth,
43 },
44 },
45 },
46 InPublic: New2B(srkTemplate),
47 }
48 createSRKRsp, err := createSRKCmd.Execute(thetpm)
49 if err != nil {
50 t.Fatalf("%v", err)
51 }
52 t.Logf("SRK name: %x", createSRKRsp.Name)
53 defer func() {
54 // Flush the SRK
55 flushSRKCmd := FlushContext{FlushHandle: createSRKRsp.ObjectHandle}
56 if _, err := flushSRKCmd.Execute(thetpm); err != nil {
57 t.Errorf("%v", err)
58 }
59 }()
60
61 // Create a sealed blob under the SRK
62 data := []byte("secrets")
63 // Include some trailing zeros to exercise the TPM's trimming of them from auth values.
64 auth := []byte("p@ssw0rd\x00\x00")
65 auth2 := []byte("p@ssw0rd")
66 createBlobCmd := Create{
67 ParentHandle: AuthHandle{
68 Handle: createSRKRsp.ObjectHandle,
69 Name: createSRKRsp.Name,
70 Auth: PasswordAuth(srkAuth),
71 },
72 InSensitive: TPM2BSensitiveCreate{
73 Sensitive: &TPMSSensitiveCreate{
74 UserAuth: TPM2BAuth{
75 Buffer: auth,
76 },
77 Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{
78 Buffer: data,
79 }),
80 },
81 },
82 InPublic: New2B(TPMTPublic{
83 Type: TPMAlgKeyedHash,
84 NameAlg: TPMAlgSHA256,
85 ObjectAttributes: TPMAObject{
86 FixedTPM: true,
87 FixedParent: true,
88 UserWithAuth: true,
89 NoDA: true,
90 },
91 }),
92 }
93
94 var createBlobRsp *CreateResponse
95
96 // Create the blob with password auth, without any session encryption
97 t.Run("Create", func(t *testing.T) {
98 createBlobRsp, err = createBlobCmd.Execute(thetpm)
99 if err != nil {
100 t.Fatalf("%v", err)
101 }
102 })
103
104 // Create the blob using an hmac auth session also for audit
105 t.Run("CreateAudit", func(t *testing.T) {
106 createBlobCmd.ParentHandle = AuthHandle{
107 Handle: createSRKRsp.ObjectHandle,
108 Name: createSRKRsp.Name,
109 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AuditExclusive()),
110 }
111
112 createBlobRsp, err = createBlobCmd.Execute(thetpm)
113 if err != nil {
114 t.Fatalf("%v", err)
115 }
116 })
117
118 // Create the blob, using the auth session also for decryption
119 t.Run("CreateDecrypt", func(t *testing.T) {
120 createBlobCmd.ParentHandle = AuthHandle{
121 Handle: createSRKRsp.ObjectHandle,
122 Name: createSRKRsp.Name,
123 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptIn)),
124 }
125 createBlobRsp, err = createBlobCmd.Execute(thetpm)
126 if err != nil {
127 t.Fatalf("%v", err)
128 }
129 })
130
131 // Create the blob, using the auth session also for encryption
132 t.Run("CreateEncrypt", func(t *testing.T) {
133 createBlobCmd.ParentHandle = AuthHandle{
134 Handle: createSRKRsp.ObjectHandle,
135 Name: createSRKRsp.Name,
136 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptOut)),
137 }
138 createBlobRsp, err = createBlobCmd.Execute(thetpm)
139 if err != nil {
140 t.Fatalf("%v", err)
141 }
142 })
143
144 // Create the blob, using the auth session also for decrypt and encrypt
145 t.Run("CreateDecryptEncrypt", func(t *testing.T) {
146 createBlobCmd.ParentHandle = AuthHandle{
147 Handle: createSRKRsp.ObjectHandle,
148 Name: createSRKRsp.Name,
149 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptInOut)),
150 }
151 createBlobRsp, err = createBlobCmd.Execute(thetpm)
152 if err != nil {
153 t.Fatalf("%v", err)
154 }
155 })
156
157 // Create the blob with decrypt and encrypt session
158 t.Run("CreateDecryptEncryptAudit", func(t *testing.T) {
159 createBlobCmd.ParentHandle = AuthHandle{
160 Handle: createSRKRsp.ObjectHandle,
161 Name: createSRKRsp.Name,
162 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth),
163 AESEncryption(128, EncryptInOut),
164 Audit()),
165 }
166 createBlobRsp, err = createBlobCmd.Execute(thetpm)
167 if err != nil {
168 t.Fatalf("%v", err)
169 }
170 })
171
172 // Create the blob with decrypt and encrypt session bound to SRK
173 t.Run("CreateDecryptEncryptSalted", func(t *testing.T) {
174 outPub, err := createSRKRsp.OutPublic.Contents()
175 if err != nil {
176 t.Fatalf("%v", err)
177 }
178 createBlobCmd.ParentHandle = AuthHandle{
179 Handle: createSRKRsp.ObjectHandle,
180 Name: createSRKRsp.Name,
181 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth),
182 AESEncryption(128, EncryptInOut),
183 Salted(createSRKRsp.ObjectHandle, *outPub)),
184 }
185 createBlobRsp, err = createBlobCmd.Execute(thetpm)
186 if err != nil {
187 t.Fatalf("%v", err)
188 }
189 })
190
191 // Use HMAC auth to authorize the rest of the Create commands
192 // Exercise re-using a use-once HMAC structure (which will spin up the session each time)
193 createBlobCmd.ParentHandle = AuthHandle{
194 Handle: createSRKRsp.ObjectHandle,
195 Name: createSRKRsp.Name,
196 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth)),
197 }
198 // Create the blob with a separate decrypt and encrypt session
199 t.Run("CreateDecryptEncryptSeparate", func(t *testing.T) {
200 createBlobRsp, err = createBlobCmd.Execute(thetpm,
201 HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)))
202 if err != nil {
203 t.Fatalf("%v", err)
204 }
205 })
206
207 // Create the blob with a separate decrypt and encrypt session, and another for audit
208 t.Run("CreateDecryptEncryptAuditSeparate", func(t *testing.T) {
209 createBlobRsp, err = createBlobCmd.Execute(thetpm,
210 HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)),
211 HMAC(TPMAlgSHA256, 16, Audit()))
212 if err != nil {
213 t.Fatalf("%v", err)
214 }
215 })
216
217 // Create the blob with a separate decrypt and encrypt session, and another for exclusive audit
218 t.Run("CreateDecryptEncryptAuditExclusiveSeparate", func(t *testing.T) {
219 createBlobRsp, err = createBlobCmd.Execute(thetpm,
220 HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)),
221 HMAC(TPMAlgSHA256, 16, AuditExclusive()))
222 if err != nil {
223 t.Fatalf("%v", err)
224 }
225 })
226
227 // Create the blob with separate decrypt and encrypt sessions.
228 t.Run("CreateDecryptEncrypt2Separate", func(t *testing.T) {
229 createBlobRsp, err = createBlobCmd.Execute(thetpm,
230 // Get weird with the algorithm and nonce choices. Mix lots of things together.
231 HMAC(TPMAlgSHA1, 20, AESEncryption(128, EncryptIn)),
232 HMAC(TPMAlgSHA384, 23, AESEncryption(128, EncryptOut)))
233 if err != nil {
234 t.Fatalf("%v", err)
235 }
236 })
237
238 // Create the blob with separate encrypt and decrypt sessions.
239 // (The TPM spec orders some extra nonces included in the first session in the order
240 // nonceTPM_decrypt, nonceTPM_encrypt, so this exercises that)
241 t.Run("CreateDecryptEncrypt2Separate", func(t *testing.T) {
242 createBlobRsp, err = createBlobCmd.Execute(thetpm,
243 HMAC(TPMAlgSHA1, 17, AESEncryption(128, EncryptOut)),
244 HMAC(TPMAlgSHA256, 32, AESEncryption(128, EncryptIn)))
245 if err != nil {
246 t.Fatalf("%v", err)
247 }
248 })
249
250 // Load the sealed blob
251 loadBlobCmd := Load{
252 ParentHandle: AuthHandle{
253 Handle: createSRKRsp.ObjectHandle,
254 Name: createSRKRsp.Name,
255 Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth)),
256 },
257 InPrivate: createBlobRsp.OutPrivate,
258 InPublic: createBlobRsp.OutPublic,
259 }
260 loadBlobRsp, err := loadBlobCmd.Execute(thetpm)
261 if err != nil {
262 t.Fatalf("%v", err)
263 }
264 defer func() {
265 // Flush the blob
266 flushBlobCmd := FlushContext{FlushHandle: loadBlobRsp.ObjectHandle}
267 if _, err := flushBlobCmd.Execute(thetpm); err != nil {
268 t.Errorf("%v", err)
269 }
270 }()
271
272 unsealCmd := Unseal{
273 ItemHandle: NamedHandle{
274 Handle: loadBlobRsp.ObjectHandle,
275 Name: loadBlobRsp.Name,
276 },
277 }
278
279 // Unseal the blob with a password session
280 t.Run("WithPassword", func(t *testing.T) {
281 unsealCmd.ItemHandle = AuthHandle{
282 Handle: loadBlobRsp.ObjectHandle,
283 Name: loadBlobRsp.Name,
284 Auth: PasswordAuth(auth),
285 }
286 unsealRsp, err := unsealCmd.Execute(thetpm)
287 if err != nil {
288 t.Errorf("%v", err)
289 }
290 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
291 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
292 }
293 })
294
295 // Unseal the blob with an incorrect password session
296 t.Run("WithWrongPassword", func(t *testing.T) {
297 unsealCmd.ItemHandle = AuthHandle{
298 Handle: loadBlobRsp.ObjectHandle,
299 Name: loadBlobRsp.Name,
300 Auth: PasswordAuth([]byte("NotThePassword")),
301 }
302 _, err := unsealCmd.Execute(thetpm)
303 if !errors.Is(err, TPMRCBadAuth) {
304 t.Errorf("want TPM_RC_BAD_AUTH, got %v", err)
305 }
306 var fmt1 TPMFmt1Error
307 if !errors.As(err, &fmt1) {
308 t.Errorf("want a Fmt1Error, got %v", err)
309 } else if isSession, session := fmt1.Session(); !isSession || session != 1 {
310 t.Errorf("want TPM_RC_BAD_AUTH on session 1, got %v", err)
311 }
312 })
313
314 // Unseal the blob with a use-once HMAC session
315 t.Run("WithHMAC", func(t *testing.T) {
316 unsealCmd.ItemHandle = AuthHandle{
317 Handle: loadBlobRsp.ObjectHandle,
318 Name: loadBlobRsp.Name,
319 Auth: HMAC(TPMAlgSHA256, 16, Auth(auth2)),
320 }
321 unsealRsp, err := unsealCmd.Execute(thetpm)
322 if err != nil {
323 t.Errorf("%v", err)
324 }
325 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
326 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
327 }
328 })
329
330 // Unseal the blob with a use-once HMAC session with encryption
331 t.Run("WithHMACEncrypt", func(t *testing.T) {
332 unsealCmd.ItemHandle = AuthHandle{
333 Handle: loadBlobRsp.ObjectHandle,
334 Name: loadBlobRsp.Name,
335 Auth: HMAC(TPMAlgSHA256, 16, Auth(auth2),
336 AESEncryption(128, EncryptOut)),
337 }
338 unsealRsp, err := unsealCmd.Execute(thetpm)
339 if err != nil {
340 t.Errorf("%v", err)
341 }
342 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
343 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
344 }
345 })
346
347 // Unseal the blob with a standalone HMAC session, re-using the session.
348 t.Run("WithHMACSession", func(t *testing.T) {
349 sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA1, 20, Auth(auth2))
350 if err != nil {
351 t.Fatalf("%v", err)
352 }
353 defer cleanup()
354 unsealCmd.ItemHandle = AuthHandle{
355 Handle: loadBlobRsp.ObjectHandle,
356 Name: loadBlobRsp.Name,
357 Auth: sess,
358 }
359
360 // It should be possible to use the session multiple times.
361 for i := 0; i < 3; i++ {
362 unsealRsp, err := unsealCmd.Execute(thetpm)
363 if err != nil {
364 t.Errorf("%v", err)
365 }
366 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
367 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
368 }
369 }
370 })
371
372 // Unseal the blob with a standalone bound HMAC session, re-using the session.
373 // Also, use session encryption.
374 t.Run("WithHMACSessionEncrypt", func(t *testing.T) {
375 sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA256, 16, Auth(auth2),
376 AESEncryption(128, EncryptOut),
377 Bound(createSRKRsp.ObjectHandle, createSRKRsp.Name, srkAuth))
378 if err != nil {
379 t.Fatalf("%v", err)
380 }
381 defer cleanup()
382 unsealCmd.ItemHandle = AuthHandle{
383 Handle: loadBlobRsp.ObjectHandle,
384 Name: loadBlobRsp.Name,
385 Auth: sess,
386 }
387
388 // It should be possible to use the session multiple times.
389 for i := 0; i < 3; i++ {
390 unsealRsp, err := unsealCmd.Execute(thetpm)
391 if err != nil {
392 t.Errorf("%v", err)
393 }
394 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
395 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
396 }
397 }
398 })
399
400 // Unseal the blob with a standalone HMAC session, re-using the session.
401 // Spin up another bound session for encryption.
402 t.Run("WithHMACSessionEncryptSeparate", func(t *testing.T) {
403 sess1, cleanup1, err := HMACSession(thetpm, TPMAlgSHA1, 16, Auth(auth2))
404 if err != nil {
405 t.Fatalf("%v", err)
406 }
407 defer cleanup1()
408 sess2, cleanup2, err := HMACSession(thetpm, TPMAlgSHA384, 16,
409 AESEncryption(128, EncryptOut),
410 Bound(createSRKRsp.ObjectHandle, createSRKRsp.Name, srkAuth))
411 if err != nil {
412 t.Fatalf("%v", err)
413 }
414 defer cleanup2()
415 unsealCmd.ItemHandle = AuthHandle{
416 Handle: loadBlobRsp.ObjectHandle,
417 Name: loadBlobRsp.Name,
418 Auth: sess1,
419 }
420
421 // It should be possible to use the sessions multiple times.
422 for i := 0; i < 3; i++ {
423 unsealRsp, err := unsealCmd.Execute(thetpm, sess2)
424 if err != nil {
425 t.Errorf("%v", err)
426 }
427 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
428 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
429 }
430 }
431 })
432 }