Debian Codegraph @

tpm2/test/testvectors/testvectors.go

raw
1 // Package testvectors contains test vectors for TPM crypto.
2 package testvectors
3
4 import (
5 _ "embed"
6 "encoding/hex"
7 "encoding/json"
8 "strings"
9 "testing"
10 )
11
12 // This package contains test vectors from https://github.com/chrisfenner/tpm-test-vectors.
13 // These test vectors were validated against the TCG Reference Implementation.
14
15 type hexBytes []byte
16
17 func (h *hexBytes) UnmarshalJSON(data []byte) error {
18 var err error
19 *h, err = hex.DecodeString(strings.Trim(string(data), "\""))
20 return err
21 }
22
23 //go:embed ecc_labeled_encaps.json
24 var eccLabeledEncapsJSON []byte
25
26 // ECCLabeledEncapsTestCase is a test case for ECC Labeled Encapsulation (Secret Sharing).
27 type ECCLabeledEncapsTestCase struct {
28 Name string
29 Description string
30 Label string
31 EphemeralPrivate hexBytes
32 PublicKey hexBytes
33 Secret hexBytes
34 Ciphertext hexBytes
35 }
36
37 // ECCLabeledEncapsulation iterates the ECC Labeled Encapsulation test cases.
38 func ECCLabeledEncapsulation(t *testing.T) []ECCLabeledEncapsTestCase {
39 t.Helper()
40
41 var testCases []ECCLabeledEncapsTestCase
42 if err := json.Unmarshal(eccLabeledEncapsJSON, &testCases); err != nil {
43 t.Fatalf("could not unmarshal JSON: %v", err)
44 }
45
46 return testCases
47 }
48
49 //go:embed rsa_labeled_encaps.json
50 var rsaLabeledEncapsJSON []byte
51
52 // RSALabeledEncapsTestCase is a test case for RSA Labeled Encapsulation (Secret Sharing).
53 type RSALabeledEncapsTestCase struct {
54 Name string
55 Description string
56 Label string
57 OAEPSalt hexBytes
58 PublicKey hexBytes
59 Secret hexBytes
60 Ciphertext hexBytes
61 }
62
63 // RSALabeledEncapsulation iterates the RSA Labeled Encapsulation test cases.
64 func RSALabeledEncapsulation(t *testing.T) []RSALabeledEncapsTestCase {
65 t.Helper()
66
67 var testCases []RSALabeledEncapsTestCase
68 if err := json.Unmarshal(rsaLabeledEncapsJSON, &testCases); err != nil {
69 t.Fatalf("could not unmarshal JSON: %v", err)
70 }
71
72 return testCases
73 }