diff --git a/attest/example_test.go b/attest/example_test.go index b9b05ff..03c5f5a 100644 --- a/attest/example_test.go +++ b/attest/example_test.go @@ -129,3 +129,48 @@ func TestExampleAIK(t *testing.T) { ExampleAIK_credentialActivation() ExampleAIK_quote() } + +func ExampleAIKPublic_Verify() { + tpm, err := attest.OpenTPM(nil) + if err != nil { + log.Fatalf("Failed to open TPM: %v", err) + } + defer tpm.Close() + + // Create a new AIK. + aik, err := tpm.NewAIK(nil) + if err != nil { + log.Fatalf("Failed to create AIK: %v", err) + } + defer aik.Close(tpm) + + // The nonce would typically be provided by the server. + nonce := []byte{1, 2, 3, 4, 5, 6, 7, 8} + + // Perform the quote & gather information necessary to verify it. + quote, err := aik.Quote(tpm, nonce, attest.HashSHA256) + if err != nil { + log.Fatalf("Failed to generate quote: %v", err) + } + pcrs, err := tpm.PCRs(attest.HashSHA256) + if err != nil { + log.Fatalf("Failed to collect PCR values: %v", err) + } + + // Construct an AIKPublic struct from the parameters of the key. + pub, err := attest.ParseAIKPublic(tpm.Version(), aik.AttestationParameters().Public) + if err != nil { + log.Fatalf("Failed to parse AIK public: %v", err) + } + + if err := pub.Verify(*quote, pcrs, nonce); err != nil { + log.Fatalf("Verification failed: %v", err) + } +} + +func TestExampleAIKPublic(t *testing.T) { + if !*testExamples { + t.SkipNow() + } + ExampleAIKPublic_Verify() +}