Debian Codegraph @

tpm2/test/hmac_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/hmac"
7 "crypto/rand"
8 "crypto/sha256"
9 "io"
10 "testing"
11
12 . "github.com/google/go-tpm/tpm2"
13 "github.com/google/go-tpm/tpm2/transport/simulator"
14 )
15
16 func TestHMAC(t *testing.T) {
17 // connect to TPM simulator
18 thetpm, err := simulator.OpenSimulator()
19 if err != nil {
20 t.Fatalf("could not connect to TPM simulator: %v", err)
21 }
22 defer thetpm.Close()
23
24 // create HMAC key
25 createPrimary := CreatePrimary{
26 PrimaryHandle: TPMRHOwner,
27 InPublic: New2B(TPMTPublic{
28 Type: TPMAlgKeyedHash,
29 NameAlg: TPMAlgSHA256,
30 ObjectAttributes: TPMAObject{
31 SignEncrypt: true,
32 FixedTPM: true,
33 FixedParent: true,
34 SensitiveDataOrigin: true,
35 UserWithAuth: true,
36 },
37 Parameters: NewTPMUPublicParms(TPMAlgKeyedHash,
38 &TPMSKeyedHashParms{
39 Scheme: TPMTKeyedHashScheme{
40 Scheme: TPMAlgHMAC,
41 Details: NewTPMUSchemeKeyedHash(TPMAlgHMAC,
42 &TPMSSchemeHMAC{
43 HashAlg: TPMAlgSHA256,
44 }),
45 },
46 }),
47 }),
48 }
49
50 rspCP, err := createPrimary.Execute(thetpm)
51 if err != nil {
52 t.Fatalf("CreatePrimary HMAC key failed: %v", err)
53 }
54
55 flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle}
56 defer func() {
57 _, _ = flushContext.Execute(thetpm)
58 }()
59
60 data := []byte("test")
61
62 hmacCmd := Hmac{
63 Handle: AuthHandle{
64 Handle: rspCP.ObjectHandle,
65 Name: rspCP.Name,
66 Auth: PasswordAuth(nil),
67 },
68 Buffer: TPM2BMaxBuffer{Buffer: data},
69 HashAlg: TPMAlgSHA256,
70 }
71
72 // HMAC Key is not exportable and cannot be known.
73 // Calculate HMAC twice and confirm they are the same.
74 hmac1, err := hmacCmd.Execute(thetpm)
75 if err != nil {
76 t.Errorf("TPM2_HMAC failed: %v", err)
77 }
78 hmac2, err := hmacCmd.Execute(thetpm)
79 if err != nil {
80 t.Errorf("TPM2_HMAC failed: %v", err)
81 }
82 if !bytes.Equal(hmac1.OutHMAC.Buffer, hmac2.OutHMAC.Buffer) {
83 t.Errorf("TPM2_HMAC failed: hmacs are different")
84 }
85 }
86
87 func TestImportedHMACKey(t *testing.T) {
88 // configurable values
89 data := []byte("input data")
90 keySensitive := []byte("the hmac key")
91 persistentHandle := TPMHandle(0x81000000)
92
93 // connect to TPM simulator
94 theTPM, err := simulator.OpenSimulator()
95 if err != nil {
96 t.Fatalf("could not connect to TPM simulator: %v", err)
97 }
98 defer theTPM.Close()
99
100 // create primary key
101 primaryKey, err := CreatePrimary{
102 PrimaryHandle: TPMRHOwner,
103 InPublic: New2B(RSASRKTemplate),
104 }.Execute(theTPM)
105
106 if err != nil {
107 t.Fatalf("could not generate SRK: %v", err)
108 }
109
110 // hmac template
111 sv := make([]byte, 32)
112 io.ReadFull(rand.Reader, sv)
113 privHash := crypto.SHA256.New()
114 privHash.Write(sv)
115 privHash.Write(keySensitive)
116
117 hmacTemplate := TPMTPublic{
118 Type: TPMAlgKeyedHash,
119 NameAlg: TPMAlgSHA256,
120 ObjectAttributes: TPMAObject{
121 UserWithAuth: true,
122 SignEncrypt: true,
123 },
124 AuthPolicy: TPM2BDigest{},
125 Parameters: NewTPMUPublicParms(TPMAlgKeyedHash,
126 &TPMSKeyedHashParms{
127 Scheme: TPMTKeyedHashScheme{
128 Scheme: TPMAlgHMAC,
129 Details: NewTPMUSchemeKeyedHash(TPMAlgHMAC,
130 &TPMSSchemeHMAC{
131 HashAlg: TPMAlgSHA256,
132 }),
133 },
134 }),
135 Unique: NewTPMUPublicID(
136 TPMAlgKeyedHash,
137 &TPM2BDigest{
138 Buffer: privHash.Sum(nil),
139 },
140 ),
141 }
142
143 // sensitive data
144 sens2B := Marshal(TPMTSensitive{
145 SensitiveType: TPMAlgKeyedHash,
146 AuthValue: TPM2BAuth{},
147 SeedValue: TPM2BDigest{
148 Buffer: sv,
149 },
150 Sensitive: NewTPMUSensitiveComposite(
151 TPMAlgKeyedHash,
152 &TPM2BSensitiveData{Buffer: keySensitive},
153 ),
154 })
155
156 l := Marshal(TPM2BPrivate{Buffer: sens2B})
157
158 // import hmac key
159 importResponse, err := Import{
160 ParentHandle: AuthHandle{
161 Handle: primaryKey.ObjectHandle,
162 Name: primaryKey.Name,
163 Auth: PasswordAuth(nil),
164 },
165 ObjectPublic: New2B(hmacTemplate),
166 Duplicate: TPM2BPrivate{Buffer: l},
167 }.Execute(theTPM)
168 if err != nil {
169 t.Fatalf("could not import hmac key: %v", err)
170 }
171
172 // load hmac key
173 hmacKey, err := Load{
174 ParentHandle: AuthHandle{
175 Handle: primaryKey.ObjectHandle,
176 Name: primaryKey.Name,
177 Auth: PasswordAuth(nil),
178 },
179 InPublic: New2B(hmacTemplate),
180 InPrivate: importResponse.OutPrivate,
181 }.Execute(theTPM)
182 if err != nil {
183 t.Fatalf("could not load hmac key: %v", err)
184 }
185
186 FlushContext{FlushHandle: primaryKey.ObjectHandle}.Execute(theTPM)
187
188 // persist hmac key
189 _, err = EvictControl{
190 Auth: TPMRHOwner,
191 ObjectHandle: &NamedHandle{
192 Handle: hmacKey.ObjectHandle,
193 Name: hmacKey.Name,
194 },
195 PersistentHandle: persistentHandle,
196 }.Execute(theTPM)
197 if err != nil {
198 t.Fatalf("could not persist hmac key: %v", err)
199 }
200
201 FlushContext{FlushHandle: hmacKey.ObjectHandle}.Execute(theTPM)
202
203 // calculate hmac using TPM
204 hmacCmd := Hmac{
205 Handle: AuthHandle{
206 Handle: persistentHandle,
207 Name: ReadPublicName(t, persistentHandle, theTPM),
208 Auth: PasswordAuth(nil),
209 },
210 Buffer: TPM2BMaxBuffer{Buffer: data},
211 HashAlg: TPMAlgSHA256,
212 }
213
214 rspHMAC, err := hmacCmd.Execute(theTPM)
215 if err != nil {
216 t.Fatalf("TPM2_HMAC failed: %v", err)
217 }
218
219 // calculate hmac in usual way
220 hmacSha256 := hmac.New(sha256.New, keySensitive)
221 hmacSha256.Write(data)
222 result := hmacSha256.Sum(nil)
223
224 // compare hmac results
225 if !bytes.Equal(result, rspHMAC.OutHMAC.Buffer) {
226 t.Errorf("want %x got %x", result, rspHMAC.OutHMAC.Buffer)
227 }
228 }