Debian Codegraph @

tpm2/marshalling_test.go

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