| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/rsa" |
| 7 |
"crypto/sha1" |
| 8 |
"crypto/sha256" |
| 9 |
"encoding/asn1" |
| 10 |
"math/big" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/google/go-tpm-tools/client" |
| 14 |
"github.com/google/go-tpm-tools/internal/test" |
| 15 |
"github.com/google/go-tpm/legacy/tpm2" |
| 16 |
) |
| 17 |
|
| 18 |
func templateSSA(hash tpm2.Algorithm) tpm2.Public { |
| 19 |
template := client.AKTemplateRSA() |
| 20 |
// Can't sign arbitrary data if restricted. |
| 21 |
template.Attributes &= ^tpm2.FlagRestricted |
| 22 |
template.RSAParameters.Sign.Hash = hash |
| 23 |
return template |
| 24 |
} |
| 25 |
|
| 26 |
func templatePSS(hash tpm2.Algorithm) tpm2.Public { |
| 27 |
template := templateSSA(hash) |
| 28 |
template.RSAParameters.Sign.Alg = tpm2.AlgRSAPSS |
| 29 |
return template |
| 30 |
} |
| 31 |
|
| 32 |
func templateECC(hash tpm2.Algorithm) tpm2.Public { |
| 33 |
template := client.AKTemplateECC() |
| 34 |
template.Attributes &= ^tpm2.FlagRestricted |
| 35 |
template.ECCParameters.Sign.Hash = hash |
| 36 |
return template |
| 37 |
} |
| 38 |
|
| 39 |
// Templates that require some sort of (default) authorization |
| 40 |
func templateAuthSSA() tpm2.Public { |
| 41 |
template := templateSSA(tpm2.AlgSHA256) |
| 42 |
template.AuthPolicy = client.DefaultEKTemplateRSA().AuthPolicy |
| 43 |
template.Attributes |= tpm2.FlagAdminWithPolicy |
| 44 |
template.Attributes &= ^tpm2.FlagUserWithAuth |
| 45 |
return template |
| 46 |
} |
| 47 |
|
| 48 |
func templateAuthECC() tpm2.Public { |
| 49 |
template := templateECC(tpm2.AlgSHA256) |
| 50 |
template.AuthPolicy = client.DefaultEKTemplateECC().AuthPolicy |
| 51 |
template.Attributes |= tpm2.FlagAdminWithPolicy |
| 52 |
template.Attributes &= ^tpm2.FlagUserWithAuth |
| 53 |
return template |
| 54 |
} |
| 55 |
|
| 56 |
func verifyRSA(pubKey crypto.PublicKey, hash crypto.Hash, digest, sig []byte) bool { |
| 57 |
return rsa.VerifyPKCS1v15(pubKey.(*rsa.PublicKey), hash, digest, sig) == nil |
| 58 |
} |
| 59 |
|
| 60 |
func verifyECC(pubKey crypto.PublicKey, _ crypto.Hash, digest, sig []byte) bool { |
| 61 |
var sigStruct struct{ R, S *big.Int } |
| 62 |
asn1.Unmarshal(sig, &sigStruct) |
| 63 |
return ecdsa.Verify(pubKey.(*ecdsa.PublicKey), digest, sigStruct.R, sigStruct.S) |
| 64 |
} |
| 65 |
|
| 66 |
func TestSign(t *testing.T) { |
| 67 |
rwc := test.GetTPM(t) |
| 68 |
defer client.CheckedClose(t, rwc) |
| 69 |
|
| 70 |
keys := []struct { |
| 71 |
name string |
| 72 |
hash crypto.Hash |
| 73 |
template tpm2.Public |
| 74 |
verify func(crypto.PublicKey, crypto.Hash, []byte, []byte) bool |
| 75 |
}{ |
| 76 |
{"RSA-SHA1", crypto.SHA1, templateSSA(tpm2.AlgSHA1), verifyRSA}, |
| 77 |
{"RSA-SHA256", crypto.SHA256, templateSSA(tpm2.AlgSHA256), verifyRSA}, |
| 78 |
{"RSA-SHA384", crypto.SHA384, templateSSA(tpm2.AlgSHA384), verifyRSA}, |
| 79 |
{"RSA-SHA512", crypto.SHA512, templateSSA(tpm2.AlgSHA512), verifyRSA}, |
| 80 |
{"ECC-SHA1", crypto.SHA1, templateECC(tpm2.AlgSHA1), verifyECC}, |
| 81 |
{"ECC-SHA256", crypto.SHA256, templateECC(tpm2.AlgSHA256), verifyECC}, |
| 82 |
{"ECC-SHA384", crypto.SHA384, templateECC(tpm2.AlgSHA384), verifyECC}, |
| 83 |
{"ECC-SHA512", crypto.SHA512, templateECC(tpm2.AlgSHA512), verifyECC}, |
| 84 |
{"Auth-RSA", crypto.SHA256, templateAuthSSA(), verifyRSA}, |
| 85 |
{"Auth-ECC", crypto.SHA256, templateAuthECC(), verifyECC}, |
| 86 |
} |
| 87 |
|
| 88 |
message := []byte("authenticated message") |
| 89 |
// Data beginning with TPM_GENERATED_VALUE (looks like a TPM-test message) |
| 90 |
generatedMsg := append([]byte("\xffTCG"), message...) |
| 91 |
for _, k := range keys { |
| 92 |
hash := k.hash.New() |
| 93 |
hash.Write(message) |
| 94 |
digest := hash.Sum(nil) |
| 95 |
alg, err := tpm2.HashToAlgorithm(k.hash) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
t.Run(k.name, func(t *testing.T) { |
| 101 |
test.SkipOnUnsupportedAlg(t, rwc, alg) |
| 102 |
|
| 103 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, k.template) |
| 104 |
if err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
defer key.Close() |
| 108 |
|
| 109 |
signer, err := key.GetSigner() |
| 110 |
if err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
sig, err := signer.Sign(nil, digest, k.hash) |
| 114 |
if err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} |
| 117 |
if !k.verify(signer.Public(), k.hash, digest, sig) { |
| 118 |
t.Error(err) |
| 119 |
} |
| 120 |
}) |
| 121 |
t.Run(k.name+"-SignData", func(t *testing.T) { |
| 122 |
test.SkipOnUnsupportedAlg(t, rwc, alg) |
| 123 |
|
| 124 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, k.template) |
| 125 |
if err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
defer key.Close() |
| 129 |
|
| 130 |
sig, err := key.SignData(message) |
| 131 |
if err != nil { |
| 132 |
t.Fatal(err) |
| 133 |
} |
| 134 |
if !k.verify(key.PublicKey(), k.hash, digest, sig) { |
| 135 |
t.Error(err) |
| 136 |
} |
| 137 |
|
| 138 |
// Unrestricted keys can sign data beginning with TPM_GENERATED_VALUE |
| 139 |
if _, err = key.SignData(generatedMsg); err != nil { |
| 140 |
t.Error(err) |
| 141 |
} |
| 142 |
}) |
| 143 |
t.Run(k.name+"-SignDataRestricted", func(t *testing.T) { |
| 144 |
test.SkipOnUnsupportedAlg(t, rwc, alg) |
| 145 |
|
| 146 |
restrictedTemplate := k.template |
| 147 |
restrictedTemplate.Attributes |= tpm2.FlagRestricted |
| 148 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, restrictedTemplate) |
| 149 |
if err != nil { |
| 150 |
t.Fatal(err) |
| 151 |
} |
| 152 |
defer key.Close() |
| 153 |
|
| 154 |
sig, err := key.SignData(message) |
| 155 |
if err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
if !k.verify(key.PublicKey(), k.hash, digest, sig) { |
| 159 |
t.Error(err) |
| 160 |
} |
| 161 |
|
| 162 |
// Restricted keys cannot sign data beginning with TPM_GENERATED_VALUE |
| 163 |
if _, err = key.SignData(generatedMsg); err == nil { |
| 164 |
t.Error("Signing TPM_GENERATED_VALUE data should fail") |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestSignIncorrectHash(t *testing.T) { |
| 171 |
rwc := test.GetTPM(t) |
| 172 |
defer client.CheckedClose(t, rwc) |
| 173 |
|
| 174 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, templateSSA(tpm2.AlgSHA256)) |
| 175 |
if err != nil { |
| 176 |
t.Fatal(err) |
| 177 |
} |
| 178 |
defer key.Close() |
| 179 |
|
| 180 |
signer, err := key.GetSigner() |
| 181 |
if err != nil { |
| 182 |
t.Fatal(err) |
| 183 |
} |
| 184 |
|
| 185 |
digestSHA1 := sha1.Sum([]byte("authenticated message")) |
| 186 |
digestSHA256 := sha256.Sum256([]byte("authenticated message")) |
| 187 |
|
| 188 |
if _, err := signer.Sign(nil, digestSHA1[:], crypto.SHA1); err == nil { |
| 189 |
t.Error("expected failure for digest and hash not matching keys sigScheme.") |
| 190 |
} |
| 191 |
|
| 192 |
if _, err := signer.Sign(nil, digestSHA1[:], crypto.SHA256); err == nil { |
| 193 |
t.Error("expected failure for correct hash, but incorrect digest.") |
| 194 |
} |
| 195 |
|
| 196 |
if _, err := signer.Sign(nil, digestSHA256[:], crypto.SHA1); err == nil { |
| 197 |
t.Error("expected failure for correct digest, but incorrect hash.") |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestSignPSS(t *testing.T) { |
| 202 |
rwc := test.GetTPM(t) |
| 203 |
defer client.CheckedClose(t, rwc) |
| 204 |
keys := []struct { |
| 205 |
name string |
| 206 |
opts crypto.SignerOpts |
| 207 |
template tpm2.Public |
| 208 |
keyBits uint16 |
| 209 |
saltLen int |
| 210 |
}{ |
| 211 |
// saltLen should be (keyBits/8) - digestSize - 2, unless that is less than |
| 212 |
// digestSize in which case, saltLen will be digestSize. |
| 213 |
// The only normal case where saltLen is not digestSize is when using |
| 214 |
// 1024 keyBits with SHA512. |
| 215 |
{"RSA-SHA1", crypto.SHA1, templatePSS(tpm2.AlgSHA1), 1024, 20}, |
| 216 |
{"RSA-SHA256", crypto.SHA256, templatePSS(tpm2.AlgSHA256), 1024, 32}, |
| 217 |
{"RSA-SHA384", crypto.SHA384, templatePSS(tpm2.AlgSHA384), 1024, 48}, |
| 218 |
{"RSA-SHA512", crypto.SHA512, templatePSS(tpm2.AlgSHA512), 1024, 62}, |
| 219 |
{"RSA-SHA512", crypto.SHA512, templatePSS(tpm2.AlgSHA512), 2048, 64}, |
| 220 |
{"RSA-SHA1", &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA1}, templatePSS(tpm2.AlgSHA1), 1024, 20}, |
| 221 |
{"RSA-SHA256", &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA256}, templatePSS(tpm2.AlgSHA256), 1024, 32}, |
| 222 |
{"RSA-SHA384", &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA384}, templatePSS(tpm2.AlgSHA384), 1024, 48}, |
| 223 |
{"RSA-SHA512", &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA512}, templatePSS(tpm2.AlgSHA512), 1024, 62}, |
| 224 |
{"RSA-SHA512", &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA512}, templatePSS(tpm2.AlgSHA512), 2048, 64}, |
| 225 |
} |
| 226 |
|
| 227 |
for _, k := range keys { |
| 228 |
t.Run(k.name, func(t *testing.T) { |
| 229 |
alg, err := tpm2.HashToAlgorithm(k.opts.HashFunc()) |
| 230 |
if err != nil { |
| 231 |
t.Fatal(err) |
| 232 |
} |
| 233 |
test.SkipOnUnsupportedAlg(t, rwc, alg) |
| 234 |
|
| 235 |
k.template.RSAParameters.KeyBits = k.keyBits |
| 236 |
|
| 237 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, k.template) |
| 238 |
if err != nil { |
| 239 |
t.Fatal(err) |
| 240 |
} |
| 241 |
defer key.Close() |
| 242 |
|
| 243 |
hash := k.opts.HashFunc().New() |
| 244 |
hash.Write([]byte("authenticated message")) |
| 245 |
digest := hash.Sum(nil) |
| 246 |
|
| 247 |
signer, err := key.GetSigner() |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
sig, err := signer.Sign(nil, digest[:], k.opts) |
| 252 |
if err != nil { |
| 253 |
t.Fatal(err) |
| 254 |
} |
| 255 |
// Different implementations may specify different salt length. Some have "keyBytes - digestSize - 2", some have |
| 256 |
// just "digestSize". Therefore here we just verify with default salt length. |
| 257 |
err = rsa.VerifyPSS(signer.Public().(*rsa.PublicKey), k.opts.HashFunc(), digest[:], sig, nil) |
| 258 |
if err != nil { |
| 259 |
t.Error(err) |
| 260 |
} |
| 261 |
}) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// Make sure signing fails when using PSS params with a non-PSS key |
| 266 |
func TestFailSignPSS(t *testing.T) { |
| 267 |
rwc := test.GetTPM(t) |
| 268 |
defer client.CheckedClose(t, rwc) |
| 269 |
keys := []struct { |
| 270 |
name string |
| 271 |
template tpm2.Public |
| 272 |
}{ |
| 273 |
{"SSA", templateSSA(tpm2.AlgSHA256)}, |
| 274 |
{"ECC", templateECC(tpm2.AlgSHA256)}, |
| 275 |
} |
| 276 |
|
| 277 |
pssOpts := rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthAuto, Hash: crypto.SHA256} |
| 278 |
|
| 279 |
for _, k := range keys { |
| 280 |
t.Run(k.name, func(t *testing.T) { |
| 281 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, k.template) |
| 282 |
if err != nil { |
| 283 |
t.Fatal(err) |
| 284 |
} |
| 285 |
defer key.Close() |
| 286 |
|
| 287 |
signer, err := key.GetSigner() |
| 288 |
if err != nil { |
| 289 |
t.Fatal(err) |
| 290 |
} |
| 291 |
|
| 292 |
// Fake SHA-256 digest |
| 293 |
digest := make([]byte, 32) |
| 294 |
if _, err = signer.Sign(nil, digest, &pssOpts); err == nil { |
| 295 |
t.Error("expected failure when using PSS options") |
| 296 |
} |
| 297 |
}) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// Signing keys without a signature scheme are incompatible with GetSigner |
| 302 |
func TestFailGetSignerNullScheme(t *testing.T) { |
| 303 |
template := templateSSA(tpm2.AlgSHA256) |
| 304 |
template.RSAParameters.Sign = nil |
| 305 |
|
| 306 |
rwc := test.GetTPM(t) |
| 307 |
defer client.CheckedClose(t, rwc) |
| 308 |
key, err := client.NewKey(rwc, tpm2.HandleEndorsement, template) |
| 309 |
if err != nil { |
| 310 |
t.Fatal(err) |
| 311 |
} |
| 312 |
defer key.Close() |
| 313 |
|
| 314 |
if _, err = key.GetSigner(); err == nil { |
| 315 |
t.Error("expected failure when calling GetSigner") |
| 316 |
} |
| 317 |
} |