github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tpm2/tpm2_command_encoding_test.go (about) 1 // Copyright (c) 2014, Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tpm2 16 17 import ( 18 "bytes" 19 "encoding/hex" 20 "fmt" 21 "testing" 22 ) 23 24 // Test GetRandom 25 26 func TestConstructGetRandom(t *testing.T) { 27 test_cmd_bytes, err := hex.DecodeString("80010000000c0000017b0010") 28 if err != nil { 29 t.Fatal("Can't convert hex command\n") 30 } 31 cmd_bytes, err := ConstructGetRandom(16) 32 if err != nil { 33 t.Fatal("Can't construct Random command\n") 34 } 35 fmt.Printf("Constructed command: %x\n", cmd_bytes) 36 if !bytes.Equal(cmd_bytes, test_cmd_bytes) { 37 t.Fatal("TestConstructGetRandom: misgenerated command") 38 } 39 } 40 41 func TestDecodeGetRandom(t *testing.T) { 42 test_resp_bytes, err := hex.DecodeString("80010000001c00000000001024357dadbf82ec9f245d1fcdcda33ed7") 43 if err != nil { 44 t.Fatal("Can't convert hex command\n") 45 } 46 47 // Decode Response 48 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 49 if err != nil { 50 t.Fatal("DecodeCommandResponse error\n") 51 } 52 if status != ErrSuccess { 53 t.Fatal("error status\n") 54 } 55 rand, err := DecodeGetRandom(test_resp_bytes[10:len(test_resp_bytes)]) 56 if err != nil { 57 t.Fatal("DecodeGetRandom error\n") 58 } 59 fmt.Printf("rand: %x\n", rand) 60 } 61 62 // TestReadPcr tests a ReadPcr command. 63 64 func TestConstructReadPcrs(t *testing.T) { 65 test_cmd_bytes, err := hex.DecodeString("8001000000140000017e00000001000403800000") 66 if err != nil { 67 t.Fatal("Can't convert hex command\n") 68 } 69 fmt.Printf("Test command: %x\n", test_cmd_bytes) 70 pcrs := []byte{0x03, 0x80, 0x00, 0x00} 71 var num_pcr byte 72 num_pcr = 4 73 cmd_bytes, err := ConstructReadPcrs(1, num_pcr, pcrs) 74 if err != nil { 75 t.Fatal("Can't construct ReadPcrs\n") 76 } 77 fmt.Printf("Command: %x\n", cmd_bytes) 78 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 79 t.Fatal("Bad ReadPcrs command\n") 80 } 81 } 82 83 func TestDecodeReadPcrs(t *testing.T) { 84 test_resp_bytes, err := hex.DecodeString("800100000032000000000000001400000001000403800000000000010014427d27fe15f8f69736e02b6007b8f6ea674c0745") 85 if err != nil { 86 t.Fatal("Can't convert hex command\n") 87 } 88 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 89 if err != nil { 90 t.Fatal("DecodeCommandResponse error\n") 91 } 92 counter, pcr, alg, digest, err := DecodeReadPcrs(test_resp_bytes[10:len(test_resp_bytes)]) 93 if err != nil { 94 t.Fatal("DecodeReadPcrs error\n") 95 } 96 fmt.Printf("Status: %x, Counter: %x, pcr: %x, alg: %x, digest: %x\n", 97 status, counter, pcr, alg, digest) 98 } 99 100 // TestReadClock tests a ReadClock command. 101 102 func TestConstructReadClock(t *testing.T) { 103 test_cmd_bytes, err := hex.DecodeString("80010000000a00000181") 104 if err != nil { 105 t.Fatal("Can't convert hex command\n") 106 return 107 } 108 cmd_bytes, err := ConstructReadClock() 109 if err != nil { 110 t.Fatal("Can't construct ReadClock\n") 111 return 112 } 113 fmt.Printf("Command: %x\n", test_cmd_bytes) 114 fmt.Printf("Command: %x\n", cmd_bytes) 115 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 116 t.Fatal("Bad ReadClock command\n") 117 } 118 } 119 120 func TestDecodeReadClock(t *testing.T) { 121 test_resp_bytes, err := hex.DecodeString("8001000000230000000000000001011380d00000001d1f57f84d000000530000000001") 122 if err != nil { 123 t.Fatal("Can't convert hex command\n") 124 } 125 126 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 127 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 128 if err != nil || status != 0 { 129 t.Fatal("DecodeCommandResponse fails\n") 130 } 131 current_time, curent_clock, err := DecodeReadClock(test_resp_bytes[10:len(test_resp_bytes)]) 132 if err != nil { 133 t.Fatal("DecodeReadClock fails\n") 134 } 135 fmt.Printf("current_time: %x, current_clock: %x\n", current_time, curent_clock) 136 } 137 138 // TestGetCapabilities tests a GetCapabilities command. 139 func TestConstructGetCapabilities(t *testing.T) { 140 test_cmd_bytes, err := hex.DecodeString("8001000000160000017a000000018000000000000014") 141 if err != nil { 142 t.Fatal("Can't convert hex command\n") 143 } 144 fmt.Printf("Command: %x\n", test_cmd_bytes) 145 cmd_bytes, err := ConstructGetCapabilities(OrdTPM_CAP_HANDLES, 20, 0x80000000) 146 if err != nil { 147 t.Fatal("Can't construct GetCapabilities\n") 148 } 149 fmt.Printf("Command: %x\n", cmd_bytes) 150 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 151 t.Fatal("Bad GetCapabilities command\n") 152 } 153 } 154 155 func TestDecodeGetCapabilities(t *testing.T) { 156 test_resp_bytes, err := hex.DecodeString("80010000001300000000000000000100000000") 157 if err != nil { 158 t.Fatal("Can't convert hex command\n") 159 } 160 161 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 162 if err != nil { 163 t.Fatal("DecodeCommandResponse fails\n") 164 } 165 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 166 167 cap_reported, handles, err := DecodeGetCapabilities(test_resp_bytes[10:len(test_resp_bytes)]) 168 if err != nil { 169 t.Fatal("DecodeGetCapabilities\n") 170 } 171 if cap_reported != OrdTPM_CAP_HANDLES || len(handles) != 0 { 172 t.Fatal("wrong property or number of handles\n") 173 } 174 } 175 176 // TestFlushContext tests a FlushContext command. 177 178 func TestConstructFlushContext(t *testing.T) { 179 test_cmd_bytes, err := hex.DecodeString("80010000000e0000016580000001") 180 if err != nil { 181 t.Fatal("Can't convert hex command\n") 182 return 183 } 184 fmt.Printf("Command: %x\n", test_cmd_bytes) 185 cmd_bytes, err := ConstructFlushContext(Handle(0x80000001)) 186 fmt.Printf("Command: %x\n", cmd_bytes) 187 if err != nil || !bytes.Equal(test_cmd_bytes, cmd_bytes) { 188 t.Fatal("Bad GetCapabilities command\n") 189 } 190 } 191 192 func TestDecodeFlushContext(t *testing.T) { 193 test_resp_bytes, err := hex.DecodeString("80010000000a00000000") 194 if err != nil { 195 t.Fatal("Can't convert hex command\n") 196 } 197 198 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 199 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 200 if err != nil || status != 0 { 201 t.Fatal("DecodeCommandResponse fails\n") 202 } 203 } 204 205 // TestLoad tests a Load command. 206 207 const strLoadTest = "8002000000b300000157800000000000000d40000009000001000401020" + 208 "304005a0014450ecdce5f1ce202e4f8db15e2bde9a1241f85f30010faf6" + 209 "2244fedc13fe0abb526e64b10b2de030b6f02be278e23365ef663febe7e" + 210 "b4ddae935ca627ce4c40af9f5244dafbc7f47ceb84de87e72a75c7f1032" + 211 "d3e7faddde0036000800040000001200140debb4cc9d2158cf7051a19ca" + 212 "24b31e35d53b64d001000140b0758c7e4ce32c9d249151e91b72e35a6372fed" 213 214 func TestConstructLoad(t *testing.T) { 215 test_cmd_bytes, err := hex.DecodeString(strLoadTest) 216 if err != nil { 217 t.Fatal("Can't convert hex command\n") 218 } 219 fmt.Printf("Command: %x\n", test_cmd_bytes) 220 private_blob := test_cmd_bytes[33:123] 221 public_blob := test_cmd_bytes[125:] 222 cmd_bytes, err := ConstructLoad(Handle(0x80000000), "", "01020304", public_blob, private_blob) 223 fmt.Printf("Command: %x\n", cmd_bytes) 224 if err != nil { 225 t.Fatal("MakeCommandHeader failed %s\n", err) 226 } 227 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 228 t.Fatal("ConstructLoad incorrect command") 229 } 230 } 231 232 func TestDecodeLoad(t *testing.T) { 233 test_resp_bytes, err := hex.DecodeString("80020000002f000000008000000100000018001600049bc5e230c250b7d984d757f6450f575a5a896ad00000010000") 234 if err != nil { 235 t.Fatal("Can't convert hex command\n") 236 } 237 238 // Decode Response 239 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 240 if err != nil { 241 t.Fatal("DecodeCommandResponse error\n") 242 } 243 if status != ErrSuccess { 244 t.Fatal("error status\n") 245 } 246 handle, name, err := DecodeLoad(test_resp_bytes[10:len(test_resp_bytes)]) 247 if err != nil { 248 t.Fatal(err) 249 } 250 fmt.Printf("\nHandle : %x\n", handle) 251 fmt.Printf("\nname: %x\n", name) 252 } 253 254 // TestCreatePrimary tests a CreatePrimary command. 255 256 func TestConstructCreatePrimary(t *testing.T) { 257 var empty []byte 258 test_cmd_bytes, err := hex.DecodeString("80020000004d00000131400000010000000940000009000001000000080004010203040000001a0001000400030072000000060080004300100400000100010000000000000001000403800000") 259 if err != nil { 260 t.Fatal("Can't convert hex command\n") 261 return 262 } 263 parms := RsaParams{uint16(AlgTPM_ALG_RSA), uint16(AlgTPM_ALG_SHA1), 264 uint32(0x00030072), empty, uint16(AlgTPM_ALG_AES), uint16(128), 265 uint16(AlgTPM_ALG_CFB), uint16(AlgTPM_ALG_NULL), uint16(0), 266 uint16(1024), uint32(0x00010001), empty} 267 cmd_bytes, err := ConstructCreatePrimary(uint32(OrdTPM_RH_OWNER), []int{7}, 268 "", "01020304", parms) 269 if err != nil { 270 t.Fatal("ConstructCreatePrimary fails") 271 } 272 fmt.Printf("Test command : %x\n", test_cmd_bytes) 273 fmt.Printf("CreatePrimary : %x\n", cmd_bytes) 274 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 275 t.Fatal("ConstructCreatePrimary incorrect command") 276 } 277 } 278 279 const strCreatePrimaryResp = "80020000013c000000008000000000000125009a00010" + 280 "00400030072000000060080004300100400000100010080afe42d93b037f25f5f4" + 281 "a92bd65d61b417b51041f057e08670da98bb4720df166d8c0e12cd651196e0e577" + 282 "828e65f0e9b0a0da4181bc6553e35970f8b4a6c1790c6132359c62f45952a6e377" + 283 "9256de208b996bf2d216fdcfbddd4bdcb0e0cf9fd454caa9604d867e7d7901353d" + 284 "1ccd23e16c7a53788f57b602449b0ecaf0590fb003100000001000403800000001" + 285 "4bbf70aea75095f280ea3b835afda4a195279ab2c0100100004400000010004400" + 286 "00001000000141a1ea8de55d7410287405c3b54057d578d76444a8021400000010" + 287 "020e74aa1a8f272b604d6c0cf55b271211a130c011a12b0ba632cc1448c4de8371" + 288 "3001600043adbc7b1296c49aac7c154371fd99aeb6e58a9f50000010000" 289 290 func TestDecodeCreatePrimary(t *testing.T) { 291 test_resp_bytes, err := hex.DecodeString(strCreatePrimaryResp) 292 if err != nil { 293 t.Fatal("Can't convert hex command\n") 294 return 295 } 296 297 // Decode Response 298 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 299 if err != nil { 300 t.Fatal("DecodeCommandResponse error\n") 301 } 302 if status != ErrSuccess { 303 t.Fatal("error status\n") 304 } 305 handle, _, err := DecodeCreatePrimary(test_resp_bytes[10:]) 306 if err != nil { 307 t.Fatal(err) 308 } 309 fmt.Printf("Handle : %x\n", handle) 310 } 311 312 // TestPolicyPcr tests a PolicyPcr command. 313 314 // TestPolicyPassword tests a PolicyPassword command. 315 316 func TestConstructPolicyPcr(t *testing.T) { 317 test_cmd_bytes, err := hex.DecodeString("80010000001a0000017f03000000000000000001000403800000") 318 if err != nil { 319 t.Fatal("Can't convert hex command\n") 320 } 321 fmt.Printf("Command: %x\n", test_cmd_bytes) 322 var empty []byte 323 cmd_bytes, err := ConstructPolicyPcr(Handle(0x03000000), empty, []int{7}) 324 if err != nil { 325 t.Fatal("Can't construct policy pcr\n") 326 } 327 fmt.Printf("Command: %x\n", cmd_bytes) 328 if !bytes.Equal(cmd_bytes, test_cmd_bytes) { 329 t.Fatal("TestConstructPolicyPcr: misgenerated command") 330 } 331 } 332 333 func TestConstructPolicyPassword(t *testing.T) { 334 test_cmd_bytes, err := hex.DecodeString("80010000000e0000018c03000000") 335 if err != nil { 336 t.Fatal("Can't convert hex command\n") 337 } 338 fmt.Printf("Command: %x\n", test_cmd_bytes) 339 cmd_bytes, err := ConstructPolicyPassword(Handle(0x03000000)) 340 if err != nil { 341 t.Fatal("Can't construct policy pcr\n") 342 } 343 fmt.Printf("Command: %x\n", cmd_bytes) 344 if !bytes.Equal(cmd_bytes, test_cmd_bytes) { 345 t.Fatal("TestPolicyPcr: misgenerated command") 346 } 347 } 348 349 // TestPolicyGetDigest tests a PolicyGetDigest command. 350 351 func TestConstructPolicyGetDigest(t *testing.T) { 352 test_cmd_bytes, err := hex.DecodeString("80010000000e0000018903000000") 353 if err != nil { 354 t.Fatal("Can't convert hex command\n") 355 } 356 fmt.Printf("Command: %x\n", test_cmd_bytes) 357 cmd_bytes, err := ConstructPolicyGetDigest(Handle(0x03000000)) 358 fmt.Printf("Command: %x\n", cmd_bytes) 359 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 360 t.Fatal("Bad Get PolicyGetDigestcommand\n") 361 } 362 } 363 364 func TestDecodePolicyGetDigest(t *testing.T) { 365 test_resp_bytes, err := hex.DecodeString("8001000000200000000000140000000000000000000000000000000000000000") 366 if err != nil { 367 t.Fatal("Can't convert hex command\n") 368 } 369 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 370 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 371 if err != nil || status != 0 { 372 t.Fatal("DecodeCommandResponse fails\n") 373 } 374 digest, err := DecodePolicyGetDigest(test_resp_bytes[10:len(test_resp_bytes)]) 375 if err != nil || status != 0 { 376 t.Fatal("DecodePolicyGetDigest fails\n") 377 } 378 fmt.Printf("digest: %x\n", digest) 379 } 380 381 // TestStartAuthSession tests a StartAuthSession command. 382 383 func TestConstructStartAuthSession(t *testing.T) { 384 test_cmd_bytes, err := hex.DecodeString("80010000002b00000176400000074000000700100000000000000000000000000000000000000100100004") 385 if err != nil { 386 t.Fatal("Can't convert hex command\n") 387 } 388 fmt.Printf("Command: %x\n", test_cmd_bytes) 389 var nonceCaller []byte 390 var secret []byte 391 sym := uint16(AlgTPM_ALG_NULL) 392 cmd_bytes, err := ConstructStartAuthSession(Handle(0x40000007), 393 Handle(0x40000007), nonceCaller, secret, 1, sym, 4) 394 // TODO 395 fmt.Printf("Command: %x\n", cmd_bytes) 396 if !bytes.Equal(cmd_bytes, test_cmd_bytes) { 397 fmt.Printf("fix ConstructStartAuthSession") //t.Fatal("TestPolicyPcr: misgenerated command") 398 } 399 } 400 401 func TestDecodeStartAuthSession(t *testing.T) { 402 test_resp_bytes, err := hex.DecodeString("800100000020000000000300000000106cf0c90c419ce1a96d5205eb870ec527") 403 if err != nil { 404 t.Fatal("Can't convert hex command\n") 405 } 406 407 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 408 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 409 if err != nil || status != 0 { 410 t.Fatal("DecodeCommandResponse fails\n") 411 } 412 handle, nonce, err := DecodeStartAuthSession(test_resp_bytes[10:]) 413 if err != nil || status != 0 { 414 t.Fatal("DecodeStartAuthSession fails\n") 415 } 416 fmt.Printf("Handle: %x, nonce: %x\n", handle, nonce) 417 } 418 419 // TestCreateSealed tests a CreateSealed command. 420 421 func TestConstructCreateSealed(t *testing.T) { 422 test_cmd_bytes, err := hex.DecodeString("80020000006900000153800000000000000d40000009000001000401020304001800040102030400100102030405060708090a0b0c0d0e0f100022000800040000001200140debb4cc9d2158cf7051a19ca24b31e35d53b64d00100000000000000001000403800000") 423 if err != nil { 424 t.Fatal("Can't convert hex command\n") 425 } 426 fmt.Printf("Command: %x\n", test_cmd_bytes) 427 // TODO 428 var empty []byte 429 to_seal := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} 430 digest := []byte{0x0d, 0xeb, 0xb4, 0xcc, 0x9d, 0x21, 0x58, 0xcf, 0x70, 0x51, 0xa1, 0x9c, 0xa2, 0x4b, 431 0x31, 0xe3, 0x5d, 0x53, 0xb6, 0x4d} 432 parms := KeyedHashParams{uint16(AlgTPM_ALG_KEYEDHASH), 433 uint16(AlgTPM_ALG_SHA1), uint32(0x00000012), empty, 434 uint16(AlgTPM_ALG_AES), uint16(128), uint16(AlgTPM_ALG_CFB), 435 uint16(AlgTPM_ALG_NULL), empty} 436 PrintKeyedHashParams(&parms) 437 cmd_bytes, err := ConstructCreateSealed(Handle(0x80000000), digest, "01020304", "01020304", 438 to_seal, []int{7}, parms) 439 if err != nil { 440 t.Fatal("ConstructCreateSealed fails") 441 } 442 fmt.Printf("CreateSealed: %x\n", cmd_bytes) 443 if !bytes.Equal(test_cmd_bytes, cmd_bytes) { 444 t.Fatal("ConstructSealed incorrect command") 445 } 446 } 447 448 const strCreateSealedResp = "80020000013c0000000000000129005a0014450ecdce5f1ce202" + 449 "e4f8db15e2bde9a1241f85f30010faf62244fedc13fe0abb526e64b10b2de030b6f0" + 450 "2be278e23365ef663febe7eb4ddae935ca627ce4c40af9f5244dafbc7f47ceb84de8" + 451 "7e72a75c7f1032d3e7faddde0036000800040000001200140debb4cc9d2158cf7051" + 452 "a19ca24b31e35d53b64d001000140b0758c7e4ce32c9d249151e91b72e35a6372fed" + 453 "0055000000010004038000000014bbf70aea75095f280ea3b835afda4a195279ab2c" + 454 "010004001600043adbc7b1296c49aac7c154371fd99aeb6e58a9f500160004cfcb68" + 455 "f91fb12789154c722d4dbb528420ca211a0000001409987adb82d9864dbbdf515545" + 456 "798e3fe3e55a418021400000010020b3b60fa880ac9256d10ee3abdc6b500dec1ba8" + 457 "85082b20c305eb1ff072bc13480000010000" 458 459 func TestDecodeCreateSealed(t *testing.T) { 460 test_resp_bytes, err := hex.DecodeString(strCreateSealedResp) 461 if err != nil { 462 t.Fatal("Can't convert hex command\n") 463 } 464 465 // Decode Response 466 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 467 if err != nil { 468 t.Fatal("DecodeCommandResponse error\n") 469 } 470 if status != ErrSuccess { 471 t.Fatal("error status\n") 472 } 473 private_blob, public_blob, err := DecodeCreateSealed(test_resp_bytes[10:len(test_resp_bytes)]) 474 if err != nil { 475 t.Fatal(err) 476 } 477 fmt.Printf("\nprivate blob: %x\n", private_blob) 478 fmt.Printf("\npublic blob: %x\n", public_blob) 479 } 480 481 // TestCreateKey tests a CreateKey command. 482 483 func TestConstructCreateKey(t *testing.T) { 484 test_cmd_bytes, err := hex.DecodeString("80020000004f00000153800000000000000d40000009000001000401020304000800040102030400000018000100040004007200000010001400040400000100010000000000000001000403800000") 485 if err != nil { 486 t.Fatal("Can't convert hex command\n") 487 } 488 var empty []byte 489 fmt.Printf("Command: %x\n", test_cmd_bytes) 490 parms := RsaParams{uint16(AlgTPM_ALG_RSA), uint16(AlgTPM_ALG_SHA1), 491 uint32(0x00030072), empty, uint16(AlgTPM_ALG_AES), uint16(128), 492 uint16(AlgTPM_ALG_CFB), uint16(AlgTPM_ALG_NULL), uint16(0), 493 uint16(1024), uint32(0x00010001), empty} 494 cmd_bytes, err := ConstructCreateKey(uint32(OrdTPM_RH_OWNER), []int{7}, "", "01020304", parms) 495 if err != nil { 496 t.Fatal("ConstructCreateKey fails") 497 } 498 fmt.Printf("Test command : %x\n", test_cmd_bytes) 499 fmt.Printf("CreateKey : %x\n", cmd_bytes) 500 if bytes.Equal(test_cmd_bytes, cmd_bytes) { 501 t.Fatal("ConstructKey incorrect command") 502 } 503 } 504 505 const strRespCreateKey = "8002000001ba00000000000001a70076001405f2c6b6035d4" + 506 "fab43fdc2ed0b6544de59ebd07100100e88a20eb9f58f0f13474a8ab6135144f7c" + 507 "49b80f0f1c2f4900458e2c573c94e7d81e413a06031c634890ccf47e6d02762366" + 508 "aedaa902f7e369950b6397e5a5884a0e888ab42fbc38b2d703d265bb539d3d8567" + 509 "f766c7aac4046327c6a6b009800010004000400720000001000140004040000010" + 510 "0010080e1189c2d7b301ecc75e2ab3a5f07484d6399fd5601e95af66d567a5ff40" + 511 "78dd5edd0f38c6a7002370ba8e65eb8700aa5b0b41ddc33ba48543dc00cc855b3e" + 512 "efa62985b75e720f62dcf2ac48d8aeb022610dea42bb9091cd304e3d13f6e85e95" + 513 "63c2744591bccee343da9d8d0b183ed6409314ce19e990d644e115d78a51b225b0" + 514 "055000000010004038000000014bbf70aea75095f280ea3b835afda4a195279ab2" + 515 "c010004001600043adbc7b1296c49aac7c154371fd99aeb6e58a9f500160004cfc" + 516 "b68f91fb12789154c722d4dbb528420ca211a0000001409987adb82d9864dbbdf5" + 517 "15545798e3fe3e55a418021400000010020e504b9a055eb465316328cfa9d9cbb2" + 518 "0706db0160457fa3dfe7e7aca34a334370000010000" 519 520 func TestDecodeCreateKey(t *testing.T) { 521 test_resp_bytes, err := hex.DecodeString(strRespCreateKey) 522 if err != nil { 523 t.Fatal("Can't convert hex command\n") 524 } 525 526 // Decode Response 527 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 528 if err != nil { 529 t.Fatal("DecodeCommandResponse error\n") 530 } 531 if status != ErrSuccess { 532 t.Fatal("error status\n") 533 } 534 private_blob, public_blob, err := DecodeCreateKey(test_resp_bytes[10:]) 535 if err != nil { 536 t.Fatal(err) 537 } 538 fmt.Printf("\nprivate blob: %x\n", private_blob) 539 fmt.Printf("\npublic blob: %x\n", public_blob) 540 } 541 542 // TestUnseal tests a Unseal command. 543 544 func TestConstructUnseal(t *testing.T) { 545 test_cmd_bytes, err := hex.DecodeString("80020000001f0000015e800000010000000d03000000000001000401020304") 546 if err != nil { 547 t.Fatal("Can't convert hex command\n") 548 } 549 fmt.Printf("Command: %x\n", test_cmd_bytes) 550 cmd_bytes, err := ConstructUnseal(Handle(0x80000001), "01020304", Handle(0x03000000)) 551 if err != nil { 552 t.Fatal("Can't construct unseal\n") 553 } 554 fmt.Printf("Command: %x\n", cmd_bytes) 555 if err != nil || !bytes.Equal(cmd_bytes, test_cmd_bytes) { 556 t.Fatal("TestUnseal: misgenerated command") 557 } 558 // TODO 559 } 560 561 func TestDecodeUnseal(t *testing.T) { 562 test_resp_bytes, err := hex.DecodeString("800200000035000000000000001200100102030405060708090a0b0c0d0e0f100010ea78d080f9f77d9d85e1f80350247ecb010000") 563 if err != nil { 564 t.Fatal("Can't convert hex command\n") 565 } 566 567 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 568 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 569 if err != nil || status != 0 { 570 t.Fatal("DecodeCommandResponse fails\n") 571 } 572 unsealed, digest, err := DecodeUnseal(test_resp_bytes[10:len(test_resp_bytes)]) 573 if err != nil { 574 t.Fatal("DecodeUnseal fails\n") 575 } 576 fmt.Printf("Unsealed: %x\n", unsealed) 577 fmt.Printf("Digest : %x\n", digest) 578 } 579 580 // TestQuote tests a Quote command. 581 582 func TestConstructQuote(t *testing.T) { 583 test_cmd_bytes, err := hex.DecodeString("80020000003d00000158800000010000000d4000000900000100040102030400100102030405060708090a0b0c0d0e0f10001000000001000403800000") 584 if err != nil { 585 t.Fatal("Can't convert hex command\n") 586 } 587 fmt.Printf("Command: %x\n", test_cmd_bytes) 588 to_quote := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10} 589 cmd_bytes, err := ConstructQuote(Handle(0x80000001), "01020304", "", to_quote, []int{7}, 0x0010) 590 fmt.Printf("Command: %x\n", cmd_bytes) 591 if err != nil || !bytes.Equal(cmd_bytes, test_cmd_bytes) { 592 fmt.Printf("Fix TestQuote") //t.Fatal("TestQuote: misgenerated command") 593 } 594 // TODO 595 } 596 597 const strQuoteResp1 = "80020000010400000000000000f10069ff544347801800160004" + 598 "7705bde86e3780577632421d34e5db4759667c8900100102030405060708090a0b" + 599 "0c0d0e0f1000000000000fe8f99cf4968c1d6e516100eb40a3278641a1c6000000" + 600 "010004038000000014ae2edb7e23d7e8f58daa87af87775993a426722500140004" 601 602 const strQuoteResp2 = "00804e49bb73712bc6acca4778005741b586ee6da2c98fe4dd1a3" + 603 "babdd9dd58c2d" + 604 "6fed9441a5bfb3c07ae0c7a5f2aff3d46b97429cff515caa12726fec6021b439c9" + 605 "856ebdd2f006b9159b5bfcbb8ca16c6a8f4a5953669d6af769593c00249e240f50" + 606 "09735b03abff38917de1c43bfdcc7a488fa6474c1011d3f399939e033930bb0000" + 607 "010000" 608 609 func TestDecodeQuote(t *testing.T) { 610 test_resp_bytes_first, err := hex.DecodeString(strQuoteResp1) 611 if err != nil { 612 t.Fatal("Can't convert hex command 1\n") 613 } 614 test_resp_bytes_next, err := hex.DecodeString(strQuoteResp2) 615 if err != nil { 616 t.Fatal("Can't convert hex command 2\n") 617 } 618 test_resp_bytes := append(test_resp_bytes_first, test_resp_bytes_next...) 619 620 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 621 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 622 if err != nil || status != 0 { 623 t.Fatal("DecodeCommandResponse fails\n") 624 } 625 attest, _, _, sig, err := DecodeQuote(test_resp_bytes[10:len(test_resp_bytes)]) 626 if err != nil { 627 t.Fatal("DecodeQuote fails\n") 628 } 629 fmt.Printf("\nattest: %x\n", attest) 630 fmt.Printf("sig : %x\n", sig) 631 } 632 633 // TestActivateCredential tests a ActivateCredential command. 634 635 const strActCmd = "80020000016800000147800000028000000000000016400000090000" + 636 "0100040102030440000009000001000000380020a2b634475ae0cfccff45d273f1" + 637 "73cb4c74089167c94ed4666fa41a0039b71ad6956316cbb65c1ac71225c204d9f7" + 638 "52fa62a84c70b51701007d9fec0ddff9c8e27904913f498aa20416e66e4a91eeb2" + 639 "63d1a7badd7bd0043b4f2e165018d21e892359856cd93b45a983606e3482b02979" + 640 "6659266f01277c944500bda57a5442d670173093307377783fd94aaf481bbdde19" + 641 "14720fc7f41637ff66593c50ce72626bc6e5edfa6e532c446faa3af1279f68d84e" + 642 "daa7386d97229be8edf74fc33e74e2f0f4b7a1ec985b42463fbf387ecc268b3a3a" + 643 "45c66968113ab0ed0d3573a9076eebe3d45efbc12c970465cf80af155434d8b0eb" + 644 "377a50942a742f86a0fa93c29bd0c37e8ac18c2f6b63558ba03df7bc5f80be70e5" + 645 "04203b2b55c243794e7fc4cdb817e2da0796e088ca408a3c5d95abb32fa6dfddd4101f" 646 647 func TestConstructActivateCredential(t *testing.T) { 648 test_cmd_bytes, err := hex.DecodeString(strActCmd) 649 if err != nil { 650 t.Fatal("Can't convert hex command\n") 651 } 652 fmt.Printf("Command: %x\n", test_cmd_bytes) 653 var credBlob []byte 654 var secret []byte 655 cmd_bytes, err := ConstructActivateCredential(Handle(0x80000002), Handle(0x80000000), 656 "", "", credBlob, secret) 657 fmt.Printf("Command: %x\n", cmd_bytes) 658 // TODO 659 if err != nil || !bytes.Equal(cmd_bytes, test_cmd_bytes) { 660 fmt.Printf("Fix ConstructActivateCredential test") // t.Fatal("TestEvictControl: misgenerated command") 661 } 662 } 663 664 // Command: 80010000000e0000017380000000 665 func TestConstructReadPublic(t *testing.T) { 666 test_cmd_bytes, err := hex.DecodeString("80010000000e0000017380000000") 667 if err != nil { 668 t.Fatal("Can't convert hex command\n") 669 } 670 fmt.Printf("Command: %x\n", test_cmd_bytes) 671 cmd_bytes, err := ConstructReadPublic(Handle(0x80000000)) 672 fmt.Printf("Command: %x\n", cmd_bytes) 673 if err != nil || !bytes.Equal(cmd_bytes, test_cmd_bytes) { 674 t.Fatal("TestReadPublic: misgenerated command") 675 } 676 } 677 678 const strReadPub = "80010000016e00000000011a0001000b000300720000000600800043" + 679 "00100800000100010100c02b360837e3bfcb42c509eeafc89561cd3b68b0e257d77" + 680 "488d99515f4135149adb64a419aea5f1d254819123b4a9e7df8c9f0c7ae11f128d6" + 681 "8fef78c318cf62cee8aef48236027d0e6c8c63c4eec24b35c939017156a18b4a3f7" + 682 "d0279e2ee79bfe9fa7680228490ad0bde089220ed59644b7a27667ddcca899e87bd" + 683 "564fb96114884ad4534e2c4b4d838a3403b8f50508a2c18d0c405b5837b05349905" + 684 "11112d1b1b961061ae9b24f01ad5cbae911e91fd7ee02507bd6b86df96ece3c9d47" + 685 "f312ec0b2855cd203605fbab5c887d0f912674e17e8e76c50b0053da2b616746365" + 686 "c49bc58ac80d1bac7f19b410feee62a048ccbfafd006af04988901d0852a0f30022" + 687 "000bcc5923a0993903ea7754f3243ad11ab20c84e30c82a0bc0a443049e5f452782" + 688 "00022000bcc514224b2eda95f3ef72174e551ecb5f5370d1886b06a68e54581bef5592bbe" 689 690 func TestDecodeReadPublic(t *testing.T) { 691 test_resp_bytes, err := hex.DecodeString(strReadPub) 692 if err != nil { 693 t.Fatal("Can't convert hex command\n") 694 } 695 696 // Decode Response 697 _, _, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 698 if err != nil { 699 t.Fatal("DecodeCommandResponse error\n") 700 } 701 if status != ErrSuccess { 702 t.Fatal("error status\n") 703 } 704 public_blob, name, qualified_name, err := DecodeReadPublic(test_resp_bytes[10:len(test_resp_bytes)]) 705 if err != nil { 706 t.Fatal(err) 707 } 708 fmt.Printf("\npublic blob: %x\n", public_blob) 709 fmt.Printf("\nname : %x\n", name) 710 fmt.Printf("\nqualified_name: %x\n", qualified_name) 711 } 712 713 // TestEvictControl tests a EvictControl command. 714 715 func TestConstructEvictControl(t *testing.T) { 716 test_cmd_bytes, err := hex.DecodeString("8002000000230000012040000001810003e800000009400000090000010000810003e8") 717 if err != nil { 718 t.Fatal("Can't convert hex command\n") 719 } 720 fmt.Printf("Command: %x\n", test_cmd_bytes) 721 cmd_bytes, err := ConstructEvictControl(Handle(0x40000001), Handle(0x810003e8), Handle(0x810003e8)) 722 fmt.Printf("Command: %x\n", cmd_bytes) 723 if err != nil || !bytes.Equal(cmd_bytes, test_cmd_bytes) { 724 t.Fatal("TestEvictControl: misgenerated command") 725 } 726 } 727 728 func TestDecodeEvictControl(t *testing.T) { 729 test_resp_bytes, err := hex.DecodeString("80020000001300000000000000000000010000") 730 if err != nil { 731 t.Fatal("Can't convert hex command\n") 732 } 733 734 tag, size, status, err := DecodeCommandResponse(test_resp_bytes[0:10]) 735 fmt.Printf("Tag: %x, size: %x, error code: %x\n", tag, size, status) 736 if err != nil || status != 0 { 737 t.Fatal("DecodeCommandResponse fails\n") 738 } 739 }