Debian Codegraph @

tpm2/marshalling_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package tpm2
2
3 import (
4 "bytes"
5 "reflect"
6 "testing"
•7 "runtime"
8
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 func TestMarshal2B(t *testing.T) {
13 // Define some TPMT_Public
14 pub := TPMTPublic{
15 Type: TPMAlgKeyedHash,
16 NameAlg: TPMAlgSHA256,
17 ObjectAttributes: TPMAObject{
18 FixedTPM: true,
19 FixedParent: true,
20 UserWithAuth: true,
21 NoDA: true,
22 },
23 }
24
25 // Get the wire-format version
26 pubBytes := Marshal(pub)
27
28 // Create two versions of the same 2B:
29 // one instantiated by the actual TPMTPublic
30 // one instantiated by the contents
31 var boxed1 TPM2BPublic
32 var boxed2 TPM2BPublic
33 boxed1 = New2B(pub)
34 boxed2 = BytesAs2B[TPMTPublic](pubBytes)
35
36 boxed1Bytes := Marshal(boxed1)
37 boxed2Bytes := Marshal(boxed2)
38
39 if !bytes.Equal(boxed1Bytes, boxed2Bytes) {
40 t.Errorf("got %x want %x", boxed2Bytes, boxed1Bytes)
41 }
42
43 z, err := Unmarshal[TPM2BPublic](boxed1Bytes)
44 if err != nil {
45 t.Fatalf("could not unmarshal TPM2BPublic: %v", err)
46 }
47 t.Logf("%v", z)
48
49 boxed3Bytes := Marshal(z)
50 if !bytes.Equal(boxed1Bytes, boxed3Bytes) {
51 t.Errorf("got %x want %x", boxed3Bytes, boxed1Bytes)
52 }
53
54 // Make a nonsense 2B_Public, demonstrating that the library doesn't have to understand the serialization
55 BytesAs2B[TPMTPublic]([]byte{0xff})
56 }
57
58 func unwrap[T any](f func() (*T, error)) *T {
59 t, err := f()
60 if err != nil {
61 panic(err.Error())
62 }
63 return t
64 }
65
66 func TestMarshalT(t *testing.T) {
67 // Define some TPMT_Public
68 pub := TPMTPublic{
69 Type: TPMAlgECC,
70 NameAlg: TPMAlgSHA256,
71 ObjectAttributes: TPMAObject{
72 SignEncrypt: true,
73 },
74 Parameters: NewTPMUPublicParms(
75 TPMAlgECC,
76 &TPMSECCParms{
77 CurveID: TPMECCNistP256,
78 },
79 ),
80 Unique: NewTPMUPublicID(
81 // This happens to be a P256 EKpub from the simulator
82 TPMAlgECC,
83 &TPMSECCPoint{
84 X: TPM2BECCParameter{},
85 Y: TPM2BECCParameter{},
86 },
87 ),
88 }
89
90 // Marshal each component of the parameters
91 symBytes := Marshal(&unwrap(pub.Parameters.ECCDetail).Symmetric)
92 t.Logf("Symmetric: %x\n", symBytes)
93 sym, err := Unmarshal[TPMTSymDefObject](symBytes)
94 if err != nil {
95 t.Fatalf("could not unmarshal TPMTSymDefObject: %v", err)
96 }
97 symBytes2 := Marshal(sym)
98 if !bytes.Equal(symBytes, symBytes2) {
99 t.Errorf("want %x\ngot %x", symBytes, symBytes2)
100 }
101 schemeBytes := Marshal(&unwrap(pub.Parameters.ECCDetail).Scheme)
102 t.Logf("Scheme: %x\n", symBytes)
103 scheme, err := Unmarshal[TPMTECCScheme](schemeBytes)
104 if err != nil {
105 t.Fatalf("could not unmarshal TPMTECCScheme: %v", err)
106 }
107 schemeBytes2 := Marshal(scheme)
108 if !bytes.Equal(schemeBytes, schemeBytes2) {
109 t.Errorf("want %x\ngot %x", schemeBytes, schemeBytes2)
110 }
111 kdfBytes := Marshal(&unwrap(pub.Parameters.ECCDetail).KDF)
112 t.Logf("KDF: %x\n", kdfBytes)
113 kdf, err := Unmarshal[TPMTKDFScheme](kdfBytes)
114 if err != nil {
115 t.Fatalf("could not unmarshal TPMTKDFScheme: %v", err)
116 }
117 kdfBytes2 := Marshal(kdf)
118 if !bytes.Equal(kdfBytes, kdfBytes2) {
119 t.Errorf("want %x\ngot %x", kdfBytes, kdfBytes2)
120 }
121
122 // Marshal the parameters
123 parmsBytes := Marshal(unwrap(pub.Parameters.ECCDetail))
124 t.Logf("Parms: %x\n", parmsBytes)
125 parms, err := Unmarshal[TPMSECCParms](parmsBytes)
126 if err != nil {
127 t.Fatalf("could not unmarshal TPMSECCParms: %v", err)
128 }
129 parmsBytes2 := Marshal(parms)
130 if !bytes.Equal(parmsBytes, parmsBytes2) {
131 t.Errorf("want %x\ngot %x", parmsBytes, parmsBytes2)
132 }
133
134 // Marshal the unique area
135 uniqueBytes := Marshal(unwrap(pub.Unique.ECC))
136 t.Logf("Unique: %x\n", uniqueBytes)
137 unique, err := Unmarshal[TPMSECCPoint](uniqueBytes)
138 if err != nil {
139 t.Fatalf("could not unmarshal TPMSECCPoint: %v", err)
140 }
141 uniqueBytes2 := Marshal(unique)
142 if !bytes.Equal(uniqueBytes, uniqueBytes2) {
143 t.Errorf("want %x\ngot %x", uniqueBytes, uniqueBytes2)
144 }
145
146 // Get the wire-format version of the whole thing
147 pubBytes := Marshal(&pub)
148
149 pub2, err := Unmarshal[TPMTPublic](pubBytes)
150 if err != nil {
151 t.Fatalf("could not unmarshal TPMTPublic: %v", err)
152 }
153
154 // Some default fields might have been populated in the round-trip. Get the wire-format again and compare.
155 pub2Bytes := Marshal(pub2)
156
157 if !bytes.Equal(pubBytes, pub2Bytes) {
158 t.Errorf("want %x\ngot %x", pubBytes, pub2Bytes)
159 }
160 }
161
162 func TestMarshalCommandResponse(t *testing.T) {
•163 if runtime.GOARCH == "s390x" {
164 t.Skip("Skip failing tests on this arch!")
165 }
166 thetpm, err := simulator.OpenSimulator()
167 if err != nil {
168 t.Fatalf("could not connect to TPM simulator: %v", err)
169 }
170 defer thetpm.Close()
171
172 getCmd := GetCapability{
173 Capability: TPMCapTPMProperties,
174 Property: uint32(TPMPTFamilyIndicator),
175 PropertyCount: 1,
176 }
177 capabilityRsp, err := getCmd.Execute(thetpm)
178 if err != nil {
179 t.Fatalf("executing GetCapability: %v", err)
180 }
181
182 cmdParamsBytes, err := MarshalCommand(getCmd)
183 if err != nil {
184 t.Fatalf("MarshalCommand failed: %v", err)
185 }
186
187 unmarshalCmd, err := UnmarshalCommand[GetCapability](cmdParamsBytes)
188 if err != nil {
189 t.Fatalf("UnmarshalCommand failed: %v", err)
190 }
191
192 if !reflect.DeepEqual(getCmd, unmarshalCmd) {
193 t.Errorf("Commands do not match \nwant: %+v\ngot: %+v", getCmd, unmarshalCmd)
194 }
195
196 respParamsBytes, err := MarshalResponse(getCmd, capabilityRsp)
197 if err != nil {
198 t.Fatalf("MarshalResponse failed: %v", err)
199 }
200
201 unmarshalRsp, err := UnmarshalResponse[GetCapabilityResponse](respParamsBytes)
202 if err != nil {
203 t.Fatalf("UnmarshalResponse failed: %v", err)
204 }
205 if !reflect.DeepEqual(capabilityRsp, unmarshalRsp) {
206 t.Errorf("Responses do not match \nwant: %+v\ngot: %+v", capabilityRsp, unmarshalRsp)
207 }
208 }