8000 add test-vector-based unit test for KDFa and KDFe by chrisfenner · Pull Request #400 · google/go-tpm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add test-vector-based unit test for KDFa and KDFe #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tpm2/test/kdfa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tpm2test

import (
"bytes"
"testing"

"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/test/testvectors"
)

func TestKDFa(t *testing.T) {
for _, testcase := range testvectors.KDFa(t) {
h, err := tpm2.TPMIAlgHash(testcase.HashAlg).Hash()
if err != nil {
t.Fatalf("%v", err)
}
t.Run(testcase.Name, func(t *testing.T) {
result := tpm2.KDFa(h, testcase.Key, testcase.Label, testcase.ContextU, testcase.ContextV, testcase.Bits)
if !bytes.Equal(result, testcase.Result) {
t.Errorf("KDFa() = %x\nwant %x", result, testcase.Result)
}
})
}
}
24 changes: 24 additions & 0 deletions tpm2/test/kdfe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tpm2test

import (
"bytes"
"testing"

"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpm2/test/testvectors"
)

func TestKDFe(t *testing.T) {
for _, testcase := range testvectors.KDFe(t) {
h, err := tpm2.TPMIAlgHash(testcase.HashAlg).Hash()
if err != nil {
t.Fatalf("%v", err)
}
t.Run(testcase.Name, func(t *testing.T) {
result := tpm2.KDFe(h, testcase.Z, testcase.Label, testcase.ContextU, testcase.ContextV, testcase.Bits)
if !bytes.Equal(result, testcase.Result) {
t.Errorf("KDFe() = %x\nwant %x", result, testcase.Result)
}
})
}
}
52 changes: 52 additions & 0 deletions tpm2/test/testvectors/testvectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,55 @@ func RSALabeledEncapsulation(t *testing.T) []RSALabeledEncapsTestCase {

return testCases
}

//go:embed kdfa.json
var kdfaJSON []byte

// KDFaTestCase is a test case for KDFa.
type KDFaTestCase struct {
Name string
HashAlg uint16
Key hexBytes
Label string
ContextU hexBytes
ContextV hexBytes
Bits int
Result hexBytes
}

func KDFa(t *testing.T) []KDFaTestCase {
t.Helper()

var testCases []KDFaTestCase
if err := json.Unmarshal(kdfaJSON, &testCases); err != nil {
t.Fatalf("could not unmarshal JSON: %v", err)
}

return testCases
}

//go:embed kdfe.json
var kdfeJSON []byte

// KDFeTestCase is a test case for KDFe.
type KDFeTestCase struct {
Name string
HashAlg uint16
Z hexBytes
Label string
ContextU hexBytes
ContextV hexBytes
Bits int
Result hexBytes
}

func KDFe(t *testing.T) []KDFeTestCase {
t.Helper()

var testCases []KDFeTestCase
if err := json.Unmarshal(kdfeJSON, &testCases); err != nil {
t.Fatalf("could not unmarshal JSON: %v", err)
}

return testCases
}
0