Debian Codegraph @

tpm2/test/read_public_test.go

raw
1 package tpm2test
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 "github.com/google/go-cmp/cmp/cmpopts"
8 . "github.com/google/go-tpm/tpm2"
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 // TestReadPublicKey compares the CreatePrimary response parameter outPublic with the output of ReadPublic outPublic.
13 func TestReadPublicKey(t *testing.T) {
14 // Open simulated TPM for testing.
15 thetpm, err := simulator.OpenSimulator()
16 if err != nil {
17 t.Fatalf("could not connect to TPM simulator: %v", err)
18 }
19
20 // Defer the close of the simulated TPM to after use.
21 // Without this, other programs/tests may not be able to get a handle to the TPM.
22 defer thetpm.Close()
23
24 // Fill in the CreatePrimary struct.
25 // See definition in Part 3, Commands, section 24.1.
26 // See tpm2/templates/go for more TPMTPublic examples.
27 createPrimary := CreatePrimary{
28 PrimaryHandle: TPMRHOwner,
29 InPublic: New2B(TPMTPublic{
30 Type: TPMAlgECC,
31 NameAlg: TPMAlgSHA256,
32 ObjectAttributes: TPMAObject{
33 FixedTPM: true,
34 FixedParent: true,
35 SensitiveDataOrigin: true,
36 UserWithAuth: true,
37 SignEncrypt: true,
38 },
39 Parameters: NewTPMUPublicParms(
40 TPMAlgECC,
41 &TPMSECCParms{
42 Scheme: TPMTECCScheme{
43 Scheme: TPMAlgECDSA,
44 Details: NewTPMUAsymScheme(
45 TPMAlgECDSA,
46 &TPMSSigSchemeECDSA{
47 HashAlg: TPMAlgSHA256,
48 },
49 ),
50 },
51 CurveID: TPMECCNistP256,
52 },
53 ),
54 }),
55 }
56
57 // Executing the command uses reflection to pack the bytes into a
58 // TPM2_CreatePrimary command, returns a TPM2_CreatePrimary Response.
59 // This response is also decoded so you are again working with structs
60 // that can be found in Part 3, Commands, section 24.1.
61 rspCP, err := createPrimary.Execute(thetpm)
62 if err != nil {
63 t.Fatalf("CreatePrimary failed: %v", err)
64 }
65
66 // The TPM can only hold so much in nonvolatile memory, thus we must
67 // flush the handle after we are done using it to prevent overloading.
68 // Again we defer the flush to after we are done using the object.
69 // It is generally good practice to defer the cleanup immediately
70 // after loading an object or creating an Authorization Session.
71 // See Part 1, Architecture, section 30.4
72 flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle}
73 defer flushContext.Execute(thetpm)
74
75 // Fill in the ReadPublic struct.
76 // See definition in Part 3, Commands, section 12.4.
77 readPublic := ReadPublic{
78 ObjectHandle: rspCP.ObjectHandle,
79 }
80
81 // Executing the command uses reflection to pack the bytes into a
82 // TPM2_ReadPublic command, returns a TPM2_ReadPublic Response.
83 // This response is also decoded so you are again working with structs
84 // that can be found in Part 3, Commands, section 12.4.
85 rspRP, err := readPublic.Execute(thetpm)
86 if err != nil {
87 t.Fatalf("ReadPublic failed: %v", err)
88 }
89
90 // PublicArea.Unique represents the unique identifier of the TPMTPublic.
91 // Notice how this test uses verification of another TPM command that is
92 // able to produce similar results to validate the response.
93 pubCreate, err := rspCP.OutPublic.Contents()
94 if err != nil {
95 t.Fatalf("%v", err)
96 }
97 pubRead, err := rspRP.OutPublic.Contents()
98 if err != nil {
99 t.Fatalf("%v", err)
100 }
101 eccCreate, err := pubCreate.Unique.ECC()
102 if err != nil {
103 t.Fatalf("%v", err)
104 }
105 eccRead, err := pubRead.Unique.ECC()
106 if err != nil {
107 t.Fatalf("%v", err)
108 }
109 if !cmp.Equal(eccCreate.X, eccRead.X, cmpopts.IgnoreUnexported(eccCreate.X)) {
110 t.Error("Mismatch between public returned from CreatePrimary & ReadPublic")
111 }
112 }
113
114 // TestReadPublicWithHMACSession tests that ReadPublic works when called with an HMAC session.
115 func TestReadPublicWithHMACSession(t *testing.T) {
116 thetpm, err := simulator.OpenSimulator()
117 if err != nil {
118 t.Fatalf("could not connect to TPM simulator: %v", err)
119 }
120 defer thetpm.Close()
121
122 createPrimaryCmd := CreatePrimary{
123 PrimaryHandle: TPMRHOwner,
124 InPublic: New2B(ECCSRKTemplate),
125 }
126
127 createRsp, err := createPrimaryCmd.Execute(thetpm)
128 if err != nil {
129 t.Fatalf("CreatePrimary failed: %v", err)
130 }
131 defer func() {
132 flushCmd := FlushContext{FlushHandle: createRsp.ObjectHandle}
133 if _, err := flushCmd.Execute(thetpm); err != nil {
134 t.Errorf("FlushContext failed: %v", err)
135 }
136 }()
137
138 t.Run("ReadPublic with HMAC session", func(t *testing.T) {
139 sess := HMAC(TPMAlgSHA256, 16 /* nonceSize */, AESEncryption(128 /* keySize */, EncryptOut))
140 readPublicCmd := ReadPublic{
141 ObjectHandle: NamedHandle{
142 Handle: createRsp.ObjectHandle,
143 Name: createRsp.Name,
144 },
145 }
146 _, err = readPublicCmd.Execute(thetpm, sess)
147 if err != nil {
148 t.Fatalf("ReadPublic failed: %v", err)
149 }
150 })
151
152 // This test ensures that migration from TPMIDHObject to handle didn't introduced a regression.
153 t.Run("ReadPublic without HMAC session", func(t *testing.T) {
154 readPublicCmd := ReadPublic{
155 ObjectHandle: createRsp.ObjectHandle,
156 }
157 _, err = readPublicCmd.Execute(thetpm)
158 if err != nil {
159 t.Fatalf("ReadPublic failed: %v", err)
160 }
161 })
162 }