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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
package tpm2test
import (
"bytes"
"errors"
"testing"
. "github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/transport/simulator"
)
// Test creating and unsealing a sealed data blob with a password and HMAC.
func TestUnseal(t *testing.T) {
templates := map[string]TPMTPublic{
"RSA": RSASRKTemplate,
"ECC": ECCSRKTemplate,
}
// Run the whole test for each of RSA and ECC SRKs.
for name, srkTemplate := range templates {
t.Run(name, func(t *testing.T) {
unsealingTest(t, srkTemplate)
})
}
}
func unsealingTest(t *testing.T, srkTemplate TPMTPublic) {
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}
defer thetpm.Close()
// Create the SRK
// Put a password on the SRK to test more of the flows.
srkAuth := []byte("mySRK")
createSRKCmd := CreatePrimary{
PrimaryHandle: TPMRHOwner,
InSensitive: TPM2BSensitiveCreate{
Sensitive: &TPMSSensitiveCreate{
UserAuth: TPM2BAuth{
Buffer: srkAuth,
},
},
},
InPublic: New2B(srkTemplate),
}
createSRKRsp, err := createSRKCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
t.Logf("SRK name: %x", createSRKRsp.Name)
defer func() {
// Flush the SRK
flushSRKCmd := FlushContext{FlushHandle: createSRKRsp.ObjectHandle}
if _, err := flushSRKCmd.Execute(thetpm); err != nil {
t.Errorf("%v", err)
}
}()
// Create a sealed blob under the SRK
data := []byte("secrets")
// Include some trailing zeros to exercise the TPM's trimming of them from auth values.
auth := []byte("p@ssw0rd\x00\x00")
auth2 := []byte("p@ssw0rd")
createBlobCmd := Create{
ParentHandle: AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: PasswordAuth(srkAuth),
},
InSensitive: TPM2BSensitiveCreate{
Sensitive: &TPMSSensitiveCreate{
UserAuth: TPM2BAuth{
Buffer: auth,
},
Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{
Buffer: data,
}),
},
},
InPublic: New2B(TPMTPublic{
Type: TPMAlgKeyedHash,
NameAlg: TPMAlgSHA256,
ObjectAttributes: TPMAObject{
FixedTPM: true,
FixedParent: true,
UserWithAuth: true,
NoDA: true,
},
}),
}
var createBlobRsp *CreateResponse
// Create the blob with password auth, without any session encryption
t.Run("Create", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob using an hmac auth session also for audit
t.Run("CreateAudit", func(t *testing.T) {
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AuditExclusive()),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob, using the auth session also for decryption
t.Run("CreateDecrypt", func(t *testing.T) {
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptIn)),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob, using the auth session also for encryption
t.Run("CreateEncrypt", func(t *testing.T) {
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptOut)),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob, using the auth session also for decrypt and encrypt
t.Run("CreateDecryptEncrypt", func(t *testing.T) {
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth), AESEncryption(128, EncryptInOut)),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with decrypt and encrypt session
t.Run("CreateDecryptEncryptAudit", func(t *testing.T) {
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth),
AESEncryption(128, EncryptInOut),
Audit()),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with decrypt and encrypt session bound to SRK
t.Run("CreateDecryptEncryptSalted", func(t *testing.T) {
outPub, err := createSRKRsp.OutPublic.Contents()
if err != nil {
t.Fatalf("%v", err)
}
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth),
AESEncryption(128, EncryptInOut),
Salted(createSRKRsp.ObjectHandle, *outPub)),
}
createBlobRsp, err = createBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
})
// Use HMAC auth to authorize the rest of the Create commands
// Exercise re-using a use-once HMAC structure (which will spin up the session each time)
createBlobCmd.ParentHandle = AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth)),
}
// Create the blob with a separate decrypt and encrypt session
t.Run("CreateDecryptEncryptSeparate", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm,
HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)))
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with a separate decrypt and encrypt session, and another for audit
t.Run("CreateDecryptEncryptAuditSeparate", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm,
HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)),
HMAC(TPMAlgSHA256, 16, Audit()))
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with a separate decrypt and encrypt session, and another for exclusive audit
t.Run("CreateDecryptEncryptAuditExclusiveSeparate", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm,
HMAC(TPMAlgSHA256, 16, AESEncryption(128, EncryptInOut)),
HMAC(TPMAlgSHA256, 16, AuditExclusive()))
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with separate decrypt and encrypt sessions.
t.Run("CreateDecryptEncrypt2Separate", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm,
// Get weird with the algorithm and nonce choices. Mix lots of things together.
HMAC(TPMAlgSHA1, 20, AESEncryption(128, EncryptIn)),
HMAC(TPMAlgSHA384, 23, AESEncryption(128, EncryptOut)))
if err != nil {
t.Fatalf("%v", err)
}
})
// Create the blob with separate encrypt and decrypt sessions.
// (The TPM spec orders some extra nonces included in the first session in the order
// nonceTPM_decrypt, nonceTPM_encrypt, so this exercises that)
t.Run("CreateDecryptEncrypt2Separate", func(t *testing.T) {
createBlobRsp, err = createBlobCmd.Execute(thetpm,
HMAC(TPMAlgSHA1, 17, AESEncryption(128, EncryptOut)),
HMAC(TPMAlgSHA256, 32, AESEncryption(128, EncryptIn)))
if err != nil {
t.Fatalf("%v", err)
}
})
// Load the sealed blob
loadBlobCmd := Load{
ParentHandle: AuthHandle{
Handle: createSRKRsp.ObjectHandle,
Name: createSRKRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(srkAuth)),
},
InPrivate: createBlobRsp.OutPrivate,
InPublic: createBlobRsp.OutPublic,
}
loadBlobRsp, err := loadBlobCmd.Execute(thetpm)
if err != nil {
t.Fatalf("%v", err)
}
defer func() {
// Flush the blob
flushBlobCmd := FlushContext{FlushHandle: loadBlobRsp.ObjectHandle}
if _, err := flushBlobCmd.Execute(thetpm); err != nil {
t.Errorf("%v", err)
}
}()
unsealCmd := Unseal{
ItemHandle: NamedHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
},
}
// Unseal the blob with a password session
t.Run("WithPassword", func(t *testing.T) {
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: PasswordAuth(auth),
}
unsealRsp, err := unsealCmd.Execute(thetpm)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
})
// Unseal the blob with an incorrect password session
t.Run("WithWrongPassword", func(t *testing.T) {
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: PasswordAuth([]byte("NotThePassword")),
}
_, err := unsealCmd.Execute(thetpm)
if !errors.Is(err, TPMRCBadAuth) {
t.Errorf("want TPM_RC_BAD_AUTH, got %v", err)
}
var fmt1 TPMFmt1Error
if !errors.As(err, &fmt1) {
t.Errorf("want a Fmt1Error, got %v", err)
} else if isSession, session := fmt1.Session(); !isSession || session != 1 {
t.Errorf("want TPM_RC_BAD_AUTH on session 1, got %v", err)
}
})
// Unseal the blob with a use-once HMAC session
t.Run("WithHMAC", func(t *testing.T) {
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(auth2)),
}
unsealRsp, err := unsealCmd.Execute(thetpm)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
})
// Unseal the blob with a use-once HMAC session with encryption
t.Run("WithHMACEncrypt", func(t *testing.T) {
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: HMAC(TPMAlgSHA256, 16, Auth(auth2),
AESEncryption(128, EncryptOut)),
}
unsealRsp, err := unsealCmd.Execute(thetpm)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
})
// Unseal the blob with a standalone HMAC session, re-using the session.
t.Run("WithHMACSession", func(t *testing.T) {
sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA1, 20, Auth(auth2))
if err != nil {
t.Fatalf("%v", err)
}
defer cleanup()
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: sess,
}
// It should be possible to use the session multiple times.
for i := 0; i < 3; i++ {
unsealRsp, err := unsealCmd.Execute(thetpm)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
}
})
// Unseal the blob with a standalone bound HMAC session, re-using the session.
// Also, use session encryption.
t.Run("WithHMACSessionEncrypt", func(t *testing.T) {
sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA256, 16, Auth(auth2),
AESEncryption(128, EncryptOut),
Bound(createSRKRsp.ObjectHandle, createSRKRsp.Name, srkAuth))
if err != nil {
t.Fatalf("%v", err)
}
defer cleanup()
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: sess,
}
// It should be possible to use the session multiple times.
for i := 0; i < 3; i++ {
unsealRsp, err := unsealCmd.Execute(thetpm)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
}
})
// Unseal the blob with a standalone HMAC session, re-using the session.
// Spin up another bound session for encryption.
t.Run("WithHMACSessionEncryptSeparate", func(t *testing.T) {
sess1, cleanup1, err := HMACSession(thetpm, TPMAlgSHA1, 16, Auth(auth2))
if err != nil {
t.Fatalf("%v", err)
}
defer cleanup1()
sess2, cleanup2, err := HMACSession(thetpm, TPMAlgSHA384, 16,
AESEncryption(128, EncryptOut),
Bound(createSRKRsp.ObjectHandle, createSRKRsp.Name, srkAuth))
if err != nil {
t.Fatalf("%v", err)
}
defer cleanup2()
unsealCmd.ItemHandle = AuthHandle{
Handle: loadBlobRsp.ObjectHandle,
Name: loadBlobRsp.Name,
Auth: sess1,
}
// It should be possible to use the sessions multiple times.
for i := 0; i < 3; i++ {
unsealRsp, err := unsealCmd.Execute(thetpm, sess2)
if err != nil {
t.Errorf("%v", err)
}
if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
}
}
})
}
|