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