| 1 |
// Copyright 2021 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
//go:build !localtest && cgo && !gofuzz |
| 16 |
// +build !localtest,cgo,!gofuzz |
| 17 |
|
| 18 |
// NOTE: simulator requires cgo, hence the build tag. |
| 19 |
|
| 20 |
package attest |
| 21 |
|
| 22 |
import ( |
| 23 |
"bytes" |
| 24 |
"crypto" |
| 25 |
"crypto/ecdsa" |
| 26 |
"crypto/elliptic" |
| 27 |
"crypto/rand" |
| 28 |
"crypto/rsa" |
| 29 |
"crypto/x509" |
| 30 |
"encoding/asn1" |
| 31 |
"math/big" |
| 32 |
"testing" |
| 33 |
|
| 34 |
"github.com/google/go-cmp/cmp" |
| 35 |
) |
| 36 |
|
| 37 |
func TestSimKeyCreateAndLoad(t *testing.T) { |
| 38 |
sim, tpm := setupSimulatedTPM(t) |
| 39 |
defer sim.Close() |
| 40 |
testKeyCreateAndLoad(t, tpm) |
| 41 |
} |
| 42 |
|
| 43 |
func TestKeyCreateAndLoad(t *testing.T) { |
| 44 |
if !*testLocal { |
| 45 |
t.SkipNow() |
| 46 |
} |
| 47 |
tpm, err := OpenTPM(nil) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 50 |
} |
| 51 |
defer tpm.Close() |
| 52 |
testKeyCreateAndLoad(t, tpm) |
| 53 |
} |
| 54 |
|
| 55 |
func testKeyCreateAndLoad(t *testing.T, tpm *TPM) { |
| 56 |
ak, err := tpm.NewAK(nil) |
| 57 |
if err != nil { |
| 58 |
t.Fatalf("NewAK() failed: %v", err) |
| 59 |
} |
| 60 |
for _, test := range []struct { |
| 61 |
name string |
| 62 |
opts *KeyConfig |
| 63 |
}{ |
| 64 |
{ |
| 65 |
name: "default", |
| 66 |
opts: nil, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "ECDSAP256-SHA256", |
| 70 |
opts: &KeyConfig{ |
| 71 |
Algorithm: ECDSA, |
| 72 |
Size: 256, |
| 73 |
}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "ECDSAP384-SHA384", |
| 77 |
opts: &KeyConfig{ |
| 78 |
Algorithm: ECDSA, |
| 79 |
Size: 384, |
| 80 |
}, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "ECDSAP521-SHA512", |
| 84 |
opts: &KeyConfig{ |
| 85 |
Algorithm: ECDSA, |
| 86 |
Size: 521, |
| 87 |
}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "RSA-1024", |
| 91 |
opts: &KeyConfig{ |
| 92 |
Algorithm: RSA, |
| 93 |
Size: 1024, |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "RSA-2048", |
| 98 |
opts: &KeyConfig{ |
| 99 |
Algorithm: RSA, |
| 100 |
Size: 2048, |
| 101 |
}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "QualifyingData-RSA", |
| 105 |
opts: &KeyConfig{ |
| 106 |
Algorithm: RSA, |
| 107 |
Size: 2048, |
| 108 |
QualifyingData: []byte("qualifying data"), |
| 109 |
}, |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "QualifyingData-ECDSA", |
| 113 |
opts: &KeyConfig{ |
| 114 |
Algorithm: ECDSA, |
| 115 |
Size: 256, |
| 116 |
QualifyingData: []byte("qualifying data"), |
| 117 |
}, |
| 118 |
}, |
| 119 |
} { |
| 120 |
t.Run(test.name, func(t *testing.T) { |
| 121 |
sk, err := tpm.NewKey(ak, test.opts) |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("NewKey() failed: %v", err) |
| 124 |
} |
| 125 |
defer sk.Close() |
| 126 |
|
| 127 |
enc, err := sk.Marshal() |
| 128 |
if err != nil { |
| 129 |
t.Fatalf("sk.Marshal() failed: %v", err) |
| 130 |
} |
| 131 |
if err := sk.Close(); err != nil { |
| 132 |
t.Fatalf("sk.Close() failed: %v", err) |
| 133 |
} |
| 134 |
|
| 135 |
loaded, err := tpm.LoadKey(enc) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("LoadKey() failed: %v", err) |
| 138 |
} |
| 139 |
defer loaded.Close() |
| 140 |
|
| 141 |
k1, k2 := sk.key.(*wrappedKey20), loaded.key.(*wrappedKey20) |
| 142 |
if !bytes.Equal(k1.public, k2.public) { |
| 143 |
t.Error("Original & loaded Key public blobs did not match.") |
| 144 |
t.Logf("Original = %v", k1.public) |
| 145 |
t.Logf("Loaded = %v", k2.public) |
| 146 |
} |
| 147 |
|
| 148 |
priv1, err := sk.Private(sk.Public()) |
| 149 |
if err != nil { |
| 150 |
t.Fatalf("sk.Private() failed: %v", err) |
| 151 |
} |
| 152 |
signer1, ok := priv1.(crypto.Signer) |
| 153 |
if !ok { |
| 154 |
t.Fatalf("want crypto.Signer, got %T", priv1) |
| 155 |
} |
| 156 |
pk1, err := x509.MarshalPKIXPublicKey(signer1.Public()) |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("cannot marshal public key: %v", err) |
| 159 |
} |
| 160 |
|
| 161 |
priv2, err := loaded.Private(loaded.Public()) |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("loaded.Private() failed: %v", err) |
| 164 |
} |
| 165 |
signer2, ok := priv2.(crypto.Signer) |
| 166 |
if !ok { |
| 167 |
t.Fatalf("want crypto.Signer, got %T", priv2) |
| 168 |
} |
| 169 |
pk2, err := x509.MarshalPKIXPublicKey(signer2.Public()) |
| 170 |
if err != nil { |
| 171 |
t.Fatalf("cannot marshal public key: %v", err) |
| 172 |
} |
| 173 |
|
| 174 |
if !bytes.Equal(pk1, pk2) { |
| 175 |
t.Error("public keys do not match") |
| 176 |
} |
| 177 |
}) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestSimKeySign(t *testing.T) { |
| 182 |
sim, tpm := setupSimulatedTPM(t) |
| 183 |
defer sim.Close() |
| 184 |
testKeySign(t, tpm) |
| 185 |
} |
| 186 |
|
| 187 |
func TestKeySign(t *testing.T) { |
| 188 |
if !*testLocal { |
| 189 |
t.SkipNow() |
| 190 |
} |
| 191 |
tpm, err := OpenTPM(nil) |
| 192 |
if err != nil { |
| 193 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 194 |
} |
| 195 |
defer tpm.Close() |
| 196 |
testKeySign(t, tpm) |
| 197 |
} |
| 198 |
|
| 199 |
func testKeySign(t *testing.T, tpm *TPM) { |
| 200 |
ak, err := tpm.NewAK(nil) |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("NewAK() failed: %v", err) |
| 203 |
} |
| 204 |
|
| 205 |
for _, test := range []struct { |
| 206 |
name string |
| 207 |
keyOpts *KeyConfig |
| 208 |
signOpts crypto.SignerOpts |
| 209 |
digest []byte |
| 210 |
}{ |
| 211 |
{ |
| 212 |
name: "default", |
| 213 |
keyOpts: nil, |
| 214 |
signOpts: nil, |
| 215 |
digest: []byte("12345678901234567890123456789012"), |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "ECDSAP256-SHA256", |
| 219 |
keyOpts: &KeyConfig{ |
| 220 |
Algorithm: ECDSA, |
| 221 |
Size: 256, |
| 222 |
}, |
| 223 |
signOpts: nil, |
| 224 |
digest: []byte("12345678901234567890123456789012"), |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "ECDSAP384-SHA384", |
| 228 |
keyOpts: &KeyConfig{ |
| 229 |
Algorithm: ECDSA, |
| 230 |
Size: 384, |
| 231 |
}, |
| 232 |
signOpts: nil, |
| 233 |
digest: []byte("123456789012345678901234567890121234567890123456"), |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "ECDSAP521-SHA512", |
| 237 |
keyOpts: &KeyConfig{ |
| 238 |
Algorithm: ECDSA, |
| 239 |
Size: 521, |
| 240 |
}, |
| 241 |
signOpts: nil, |
| 242 |
digest: []byte("1234567890123456789012345678901212345678901234567890123456789012"), |
| 243 |
}, |
| 244 |
{ |
| 245 |
name: "RSA2048-PKCS1v15-SHA256", |
| 246 |
keyOpts: &KeyConfig{ |
| 247 |
Algorithm: RSA, |
| 248 |
Size: 2048, |
| 249 |
}, |
| 250 |
signOpts: crypto.SHA256, |
| 251 |
digest: []byte("12345678901234567890123456789012"), |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "RSA2048-PKCS1v15-SHA384", |
| 255 |
keyOpts: &KeyConfig{ |
| 256 |
Algorithm: RSA, |
| 257 |
Size: 2048, |
| 258 |
}, |
| 259 |
signOpts: crypto.SHA384, |
| 260 |
digest: []byte("123456789012345678901234567890121234567890123456"), |
| 261 |
}, |
| 262 |
{ |
| 263 |
name: "RSA2048-PKCS1v15-SHA512", |
| 264 |
keyOpts: &KeyConfig{ |
| 265 |
Algorithm: RSA, |
| 266 |
Size: 2048, |
| 267 |
}, |
| 268 |
signOpts: crypto.SHA512, |
| 269 |
digest: []byte("1234567890123456789012345678901212345678901234567890123456789012"), |
| 270 |
}, |
| 271 |
{ |
| 272 |
name: "RSA2048-PSS-SHA256", |
| 273 |
keyOpts: &KeyConfig{ |
| 274 |
Algorithm: RSA, |
| 275 |
Size: 2048, |
| 276 |
}, |
| 277 |
signOpts: &rsa.PSSOptions{ |
| 278 |
SaltLength: rsa.PSSSaltLengthAuto, |
| 279 |
Hash: crypto.SHA256, |
| 280 |
}, |
| 281 |
digest: []byte("12345678901234567890123456789012"), |
| 282 |
}, |
| 283 |
{ |
| 284 |
name: "RSA2048-PSS-SHA384", |
| 285 |
keyOpts: &KeyConfig{ |
| 286 |
Algorithm: RSA, |
| 287 |
Size: 2048, |
| 288 |
}, |
| 289 |
signOpts: &rsa.PSSOptions{ |
| 290 |
SaltLength: rsa.PSSSaltLengthAuto, |
| 291 |
Hash: crypto.SHA384, |
| 292 |
}, |
| 293 |
digest: []byte("123456789012345678901234567890121234567890123456"), |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "RSA2048-PSS-SHA512", |
| 297 |
keyOpts: &KeyConfig{ |
| 298 |
Algorithm: RSA, |
| 299 |
Size: 2048, |
| 300 |
}, |
| 301 |
signOpts: &rsa.PSSOptions{ |
| 302 |
SaltLength: rsa.PSSSaltLengthAuto, |
| 303 |
Hash: crypto.SHA512, |
| 304 |
}, |
| 305 |
digest: []byte("1234567890123456789012345678901212345678901234567890123456789012"), |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "RSA2048-PSS-SHA256, PSSSaltLengthEqualsHash", |
| 309 |
keyOpts: &KeyConfig{ |
| 310 |
Algorithm: RSA, |
| 311 |
Size: 2048, |
| 312 |
}, |
| 313 |
signOpts: &rsa.PSSOptions{ |
| 314 |
SaltLength: rsa.PSSSaltLengthEqualsHash, |
| 315 |
Hash: crypto.SHA256, |
| 316 |
}, |
| 317 |
digest: []byte("12345678901234567890123456789012"), |
| 318 |
}, |
| 319 |
{ |
| 320 |
name: "RSA2048-PSS-SHA256, explicit salt len", |
| 321 |
keyOpts: &KeyConfig{ |
| 322 |
Algorithm: RSA, |
| 323 |
Size: 2048, |
| 324 |
}, |
| 325 |
signOpts: &rsa.PSSOptions{ |
| 326 |
SaltLength: 32, |
| 327 |
Hash: crypto.SHA256, |
| 328 |
}, |
| 329 |
digest: []byte("12345678901234567890123456789012"), |
| 330 |
}, |
| 331 |
{ |
| 332 |
name: "RSA2048-PSS-SHA384, explicit salt len", |
| 333 |
keyOpts: &KeyConfig{ |
| 334 |
Algorithm: RSA, |
| 335 |
Size: 2048, |
| 336 |
}, |
| 337 |
signOpts: &rsa.PSSOptions{ |
| 338 |
SaltLength: 48, |
| 339 |
Hash: crypto.SHA384, |
| 340 |
}, |
| 341 |
digest: []byte("123456789012345678901234567890121234567890123456"), |
| 342 |
}, |
| 343 |
{ |
| 344 |
name: "RSA2048-PSS-SHA512, explicit salt len", |
| 345 |
keyOpts: &KeyConfig{ |
| 346 |
Algorithm: RSA, |
| 347 |
Size: 2048, |
| 348 |
}, |
| 349 |
signOpts: &rsa.PSSOptions{ |
| 350 |
SaltLength: 64, |
| 351 |
Hash: crypto.SHA512, |
| 352 |
}, |
| 353 |
digest: []byte("1234567890123456789012345678901212345678901234567890123456789012"), |
| 354 |
}, |
| 355 |
} { |
| 356 |
t.Run(test.name, func(t *testing.T) { |
| 357 |
sk, err := tpm.NewKey(ak, test.keyOpts) |
| 358 |
if err != nil { |
| 359 |
t.Fatalf("NewKey() failed: %v", err) |
| 360 |
} |
| 361 |
defer sk.Close() |
| 362 |
|
| 363 |
pub := sk.Public() |
| 364 |
priv, err := sk.Private(pub) |
| 365 |
if err != nil { |
| 366 |
t.Fatalf("sk.Private() failed: %v", err) |
| 367 |
} |
| 368 |
signer, ok := priv.(crypto.Signer) |
| 369 |
if !ok { |
| 370 |
t.Fatalf("want crypto.Signer, got %T", priv) |
| 371 |
} |
| 372 |
sig, err := signer.Sign(rand.Reader, test.digest, test.signOpts) |
| 373 |
if err != nil { |
| 374 |
t.Fatalf("signer.Sign() failed: %v", err) |
| 375 |
} |
| 376 |
|
| 377 |
if test.keyOpts == nil || test.keyOpts.Algorithm == ECDSA { |
| 378 |
verifyECDSA(t, pub, test.digest, sig) |
| 379 |
} else { |
| 380 |
verifyRSA(t, pub, test.digest, sig, test.signOpts) |
| 381 |
} |
| 382 |
}) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func verifyECDSA(t *testing.T, pub crypto.PublicKey, digest, sig []byte) { |
| 387 |
t.Helper() |
| 388 |
parsed := struct{ R, S *big.Int }{} |
| 389 |
_, err := asn1.Unmarshal(sig, &parsed) |
| 390 |
if err != nil { |
| 391 |
t.Fatalf("signature parsing failed: %v", err) |
| 392 |
} |
| 393 |
pubECDSA, ok := pub.(*ecdsa.PublicKey) |
| 394 |
if !ok { |
| 395 |
t.Fatalf("want *ecdsa.PublicKey, got %T", pub) |
| 396 |
} |
| 397 |
if !ecdsa.Verify(pubECDSA, digest[:], parsed.R, parsed.S) { |
| 398 |
t.Fatalf("ecdsa.Verify() failed") |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
func verifyRSA(t *testing.T, pub crypto.PublicKey, digest, sig []byte, opts crypto.SignerOpts) { |
| 403 |
t.Helper() |
| 404 |
pubRSA, ok := pub.(*rsa.PublicKey) |
| 405 |
if !ok { |
| 406 |
t.Fatalf("want *rsa.PublicKey, got %T", pub) |
| 407 |
} |
| 408 |
if pss, ok := opts.(*rsa.PSSOptions); ok { |
| 409 |
if err := rsa.VerifyPSS(pubRSA, opts.HashFunc(), digest, sig, pss); err != nil { |
| 410 |
t.Fatalf("rsa.VerifyPSS(): %v", err) |
| 411 |
} |
| 412 |
} else { |
| 413 |
if err := rsa.VerifyPKCS1v15(pubRSA, opts.HashFunc(), digest, sig); err != nil { |
| 414 |
t.Fatalf("signature verification failed: %v", err) |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
func TestSimKeySignErrors(t *testing.T) { |
| 420 |
testKeySignErrors(t, true) |
| 421 |
} |
| 422 |
|
| 423 |
func TestKeySignErrors(t *testing.T) { |
| 424 |
if !*testLocal { |
| 425 |
t.SkipNow() |
| 426 |
} |
| 427 |
testKeySignErrors(t, false) |
| 428 |
} |
| 429 |
|
| 430 |
func testKeySignErrors(t *testing.T, sim bool) { |
| 431 |
for _, test := range []struct { |
| 432 |
name string |
| 433 |
keyOpts *KeyConfig |
| 434 |
signOpts crypto.SignerOpts |
| 435 |
digest []byte |
| 436 |
}{ |
| 437 |
{ |
| 438 |
name: "ECDSAP256-SHA256 - Short Digest", |
| 439 |
keyOpts: &KeyConfig{ |
| 440 |
Algorithm: ECDSA, |
| 441 |
Size: 256, |
| 442 |
}, |
| 443 |
signOpts: nil, |
| 444 |
digest: []byte("12345678901234567890"), |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "RSA2048-PKCS1v15-SHA256 - Short Digest", |
| 448 |
keyOpts: &KeyConfig{ |
| 449 |
Algorithm: RSA, |
| 450 |
Size: 2048, |
| 451 |
}, |
| 452 |
signOpts: crypto.SHA256, |
| 453 |
digest: []byte("12345678901234567890"), |
| 454 |
}, |
| 455 |
{ |
| 456 |
name: "ECDSAP256 with RSA PSS Options", |
| 457 |
keyOpts: &KeyConfig{ |
| 458 |
Algorithm: ECDSA, |
| 459 |
Size: 256, |
| 460 |
}, |
| 461 |
signOpts: &rsa.PSSOptions{ |
| 462 |
SaltLength: rsa.PSSSaltLengthAuto, |
| 463 |
Hash: crypto.SHA256, |
| 464 |
}, |
| 465 |
digest: []byte("12345678901234567890123456789012"), |
| 466 |
}, |
| 467 |
{ |
| 468 |
name: "RSA2048-PKCS1v15-SHA256 - Mismatched Hash Opts", |
| 469 |
keyOpts: &KeyConfig{ |
| 470 |
Algorithm: RSA, |
| 471 |
Size: 2048, |
| 472 |
}, |
| 473 |
signOpts: crypto.SHA384, |
| 474 |
digest: []byte("12345678901234567890123456789012"), |
| 475 |
}, |
| 476 |
{ |
| 477 |
name: "RSA2048-PSS-SHA256 - Mismatched Hash Opts", |
| 478 |
keyOpts: &KeyConfig{ |
| 479 |
Algorithm: RSA, |
| 480 |
Size: 2048, |
| 481 |
}, |
| 482 |
signOpts: &rsa.PSSOptions{ |
| 483 |
SaltLength: rsa.PSSSaltLengthAuto, |
| 484 |
Hash: crypto.SHA384, |
| 485 |
}, |
| 486 |
digest: []byte("12345678901234567890123456789012"), |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "RSA2048-PSS-SHA256 - Mismatched Salt Length", |
| 490 |
keyOpts: &KeyConfig{ |
| 491 |
Algorithm: RSA, |
| 492 |
Size: 2048, |
| 493 |
}, |
| 494 |
signOpts: &rsa.PSSOptions{ |
| 495 |
SaltLength: 16, |
| 496 |
Hash: crypto.SHA256, |
| 497 |
}, |
| 498 |
digest: []byte("12345678901234567890123456789012"), |
| 499 |
}, |
| 500 |
} { |
| 501 |
t.Run(test.name, func(t *testing.T) { |
| 502 |
var tpm *TPM |
| 503 |
var err error |
| 504 |
if sim { |
| 505 |
s, tmpTpm := setupSimulatedTPM(t) |
| 506 |
defer s.Close() |
| 507 |
tpm = tmpTpm |
| 508 |
} else { |
| 509 |
tpm, err = OpenTPM(nil) |
| 510 |
if err != nil { |
| 511 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 512 |
} |
| 513 |
defer tpm.Close() |
| 514 |
} |
| 515 |
|
| 516 |
ak, err := tpm.NewAK(nil) |
| 517 |
if err != nil { |
| 518 |
t.Fatalf("NewAK() failed: %v", err) |
| 519 |
} |
| 520 |
defer ak.Close(tpm) |
| 521 |
|
| 522 |
sk, err := tpm.NewKey(ak, test.keyOpts) |
| 523 |
if err != nil { |
| 524 |
t.Fatalf("NewKey() failed: %v", err) |
| 525 |
} |
| 526 |
defer sk.Close() |
| 527 |
|
| 528 |
pub := sk.Public() |
| 529 |
priv, err := sk.Private(pub) |
| 530 |
if err != nil { |
| 531 |
t.Fatalf("sk.Private() failed: %v", err) |
| 532 |
} |
| 533 |
signer, ok := priv.(crypto.Signer) |
| 534 |
if !ok { |
| 535 |
t.Fatalf("want crypto.Signer, got %T", priv) |
| 536 |
} |
| 537 |
_, err = signer.Sign(rand.Reader, test.digest, test.signOpts) |
| 538 |
if err == nil { |
| 539 |
t.Errorf("signer.Sign() succeeded, want error") |
| 540 |
} |
| 541 |
}) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
func TestSimKeyOpts(t *testing.T) { |
| 546 |
sim, tpm := setupSimulatedTPM(t) |
| 547 |
defer sim.Close() |
| 548 |
testKeyOpts(t, tpm) |
| 549 |
} |
| 550 |
|
| 551 |
func TestKeyOpts(t *testing.T) { |
| 552 |
if !*testLocal { |
| 553 |
t.SkipNow() |
| 554 |
} |
| 555 |
tpm, err := OpenTPM(nil) |
| 556 |
if err != nil { |
| 557 |
t.Fatalf("OpenTPM() failed: %v", err) |
| 558 |
} |
| 559 |
defer tpm.Close() |
| 560 |
testKeyOpts(t, tpm) |
| 561 |
} |
| 562 |
|
| 563 |
func testKeyOpts(t *testing.T, tpm *TPM) { |
| 564 |
ak, err := tpm.NewAK(nil) |
| 565 |
if err != nil { |
| 566 |
t.Fatalf("NewAK() failed: %v", err) |
| 567 |
} |
| 568 |
for _, test := range []struct { |
| 569 |
name string |
| 570 |
opts *KeyConfig |
| 571 |
err bool |
| 572 |
}{ |
| 573 |
{ |
| 574 |
name: "wrong alg", |
| 575 |
opts: &KeyConfig{ |
| 576 |
Algorithm: "fake alg", |
| 577 |
}, |
| 578 |
err: true, |
| 579 |
}, |
| 580 |
{ |
| 581 |
name: "wrong size", |
| 582 |
opts: &KeyConfig{ |
| 583 |
Algorithm: ECDSA, |
| 584 |
Size: 1234, |
| 585 |
}, |
| 586 |
err: true, |
| 587 |
}, |
| 588 |
{ |
| 589 |
name: "default", |
| 590 |
opts: nil, |
| 591 |
err: false, |
| 592 |
}, |
| 593 |
{ |
| 594 |
name: "ECDSAP256", |
| 595 |
opts: &KeyConfig{ |
| 596 |
Algorithm: ECDSA, |
| 597 |
Size: 256, |
| 598 |
}, |
| 599 |
err: false, |
| 600 |
}, |
| 601 |
{ |
| 602 |
name: "ECDSAP384", |
| 603 |
opts: &KeyConfig{ |
| 604 |
Algorithm: ECDSA, |
| 605 |
Size: 384, |
| 606 |
}, |
| 607 |
err: false, |
| 608 |
}, |
| 609 |
{ |
| 610 |
name: "ECDSAP521", |
| 611 |
opts: &KeyConfig{ |
| 612 |
Algorithm: ECDSA, |
| 613 |
Size: 521, |
| 614 |
}, |
| 615 |
err: false, |
| 616 |
}, |
| 617 |
{ |
| 618 |
name: "RSA-1024", |
| 619 |
opts: &KeyConfig{ |
| 620 |
Algorithm: RSA, |
| 621 |
Size: 1024, |
| 622 |
}, |
| 623 |
err: false, |
| 624 |
}, |
| 625 |
{ |
| 626 |
name: "RSA-2048", |
| 627 |
opts: &KeyConfig{ |
| 628 |
Algorithm: RSA, |
| 629 |
Size: 2048, |
| 630 |
}, |
| 631 |
err: false, |
| 632 |
}, |
| 633 |
} { |
| 634 |
t.Run(test.name, func(t *testing.T) { |
| 635 |
sk, err := tpm.NewKey(ak, test.opts) |
| 636 |
if !test.err && err != nil { |
| 637 |
t.Fatalf("NewKey() failed: %v", err) |
| 638 |
} |
| 639 |
if test.err { |
| 640 |
if err == nil { |
| 641 |
sk.Close() |
| 642 |
t.Fatalf("NewKey(): expected err != nil") |
| 643 |
} |
| 644 |
return |
| 645 |
} |
| 646 |
defer sk.Close() |
| 647 |
|
| 648 |
expected := test.opts |
| 649 |
if expected == nil { |
| 650 |
expected = defaultConfig |
| 651 |
} |
| 652 |
|
| 653 |
switch pub := sk.Public().(type) { |
| 654 |
case *ecdsa.PublicKey: |
| 655 |
if expected.Algorithm != ECDSA { |
| 656 |
t.Errorf("incorrect key type generated, expected %q, got EC", expected.Algorithm) |
| 657 |
} |
| 658 |
sizeToCurve := map[int]elliptic.Curve{ |
| 659 |
256: elliptic.P256(), |
| 660 |
384: elliptic.P384(), |
| 661 |
521: elliptic.P521(), |
| 662 |
} |
| 663 |
expectedCurve, ok := sizeToCurve[expected.Size] |
| 664 |
if !ok { |
| 665 |
t.Fatalf("cannot match curve to key size %d", expected.Size) |
| 666 |
} |
| 667 |
if expectedCurve != pub.Curve { |
| 668 |
t.Errorf("incorrect curve, expected %v, got %v", expectedCurve, pub.Curve) |
| 669 |
} |
| 670 |
case *rsa.PublicKey: |
| 671 |
if expected.Algorithm != RSA { |
| 672 |
t.Errorf("incorrect key type, expected %q, got RSA", expected.Algorithm) |
| 673 |
} |
| 674 |
if pub.Size()*8 != expected.Size { |
| 675 |
t.Errorf("incorrect key size, expected %d, got %d", expected.Size, pub.Size()*8) |
| 676 |
} |
| 677 |
default: |
| 678 |
t.Errorf("unsupported key type: %T", pub) |
| 679 |
} |
| 680 |
}) |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
func TestOptsSetDefault(t *testing.T) { |
| 685 |
if optsSetDefault(nil) != defaultConfig { |
| 686 |
t.Errorf("expecting optsSetDefault(nil) to return the defaultConfig") |
| 687 |
} |
| 688 |
|
| 689 |
normalOpts := KeyConfig{ |
| 690 |
Algorithm: RSA, |
| 691 |
Size: 2048, |
| 692 |
} |
| 693 |
if optsSetDefault(&normalOpts) != &normalOpts { |
| 694 |
t.Errorf("expecting optsSetDefault() to return the same pointer for normal options") |
| 695 |
} |
| 696 |
|
| 697 |
optsMissingAlgorithmAndSize := KeyConfig{ |
| 698 |
QualifyingData: []byte("hello"), |
| 699 |
} |
| 700 |
optsAfter := optsSetDefault(&optsMissingAlgorithmAndSize) |
| 701 |
if optsAfter == &optsMissingAlgorithmAndSize { |
| 702 |
t.Errorf("expecting optsSetDefault() to return a pointer to a copy") |
| 703 |
} |
| 704 |
if diff := cmp.Diff(optsAfter, &KeyConfig{Algorithm: ECDSA, Size: 256, QualifyingData: []byte("hello")}); diff != "" { |
| 705 |
t.Errorf("optsSetDefault() mismatch (-want +got):\n%s", diff) |
| 706 |
} |
| 707 |
} |