Debian Codegraph @

tpm2/test/load_external_test.go

raw
1 package tpm2test
2
3 import (
4 "encoding/hex"
5 "testing"
6
7 . "github.com/google/go-tpm/tpm2"
8 "github.com/google/go-tpm/tpm2/transport/simulator"
9 )
10
11 func decodeHex(t *testing.T, h string) []byte {
12 t.Helper()
13 data, err := hex.DecodeString(h)
14 if err != nil {
15 t.Fatalf("could not decode '%v' as hex data: %v", h, err)
16 }
17 return data
18 }
19
20 func TestLoadExternal(t *testing.T) {
21 loads := map[string]*LoadExternal{
22 "ECCNoSensitive": {
23 InPublic: New2B(TPMTPublic{
24 Type: TPMAlgECC,
25 NameAlg: TPMAlgSHA256,
26 ObjectAttributes: TPMAObject{
27 SignEncrypt: true,
28 },
29 Parameters: NewTPMUPublicParms(
30 TPMAlgECC,
31 &TPMSECCParms{
32 CurveID: TPMECCNistP256,
33 },
34 ),
35 Unique: NewTPMUPublicID(
36 // This happens to be a P256 EKpub from the simulator
37 TPMAlgECC,
38 &TPMSECCPoint{
39 X: TPM2BECCParameter{Buffer: decodeHex(t, "9855efa3514873b88067ab127b2d4692864a395db3d9e4ccad0592478a245c16")},
40 Y: TPM2BECCParameter{Buffer: decodeHex(t, "e802a26649839a2d7b13c812a5dc0b61c110cbe62db784d96e60a823448c8993")},
41 },
42 ),
43 }),
44 },
45 "KeyedHashSensitive": {
46 InPrivate: New2B(
47 TPMTSensitive{
48 SensitiveType: TPMAlgKeyedHash,
49 SeedValue: TPM2BDigest{
50 Buffer: []byte("obfuscation is my middle name!!!"),
51 },
52 Sensitive: NewTPMUSensitiveComposite(
53 TPMAlgKeyedHash,
54 &TPM2BSensitiveData{
55 Buffer: []byte("secrets"),
56 },
57 ),
58 }),
59 InPublic: New2B(
60 TPMTPublic{
61 Type: TPMAlgKeyedHash,
62 NameAlg: TPMAlgSHA256,
63 Unique: NewTPMUPublicID(
64 TPMAlgKeyedHash,
65 &TPM2BDigest{
66 // SHA256("obfuscation is my middle name!!!secrets")
67 Buffer: decodeHex(t, "ed4fe8e2bff97665e7bfbe27c2365d07a9be91dd92d997cd91cc706b6074eb08"),
68 },
69 ),
70 }),
71 },
72 }
73
74 thetpm, err := simulator.OpenSimulator()
75 if err != nil {
76 t.Fatalf("could not connect to TPM simulator: %v", err)
77 }
78 defer thetpm.Close()
79
80 for name, load := range loads {
81 t.Run(name, func(t *testing.T) {
82 rsp, err := load.Execute(thetpm)
83 if err != nil {
84 t.Fatalf("error from LoadExternal: %v", err)
85 }
86 if _, err = (FlushContext{FlushHandle: rsp.ObjectHandle}).Execute(thetpm); err != nil {
87 t.Errorf("error from FlushContext: %v", err)
88 }
89 })
90 }
91 }