-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublic_template_test.go
153 lines (133 loc) · 3.44 KB
/
public_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package pgtpm_test
import (
"encoding/json"
"io/ioutil"
"math/big"
"reflect"
"testing"
"github.com/google/go-tpm/tpm2"
"github.com/paulgriffiths/pgtpm"
)
func TestPublicTemplateUnmarshalJSON(t *testing.T) {
t.Parallel()
var testcases = []struct {
filename string
want pgtpm.PublicTemplate
}{
{
filename: "testdata/public_template_1.json",
want: pgtpm.PublicTemplate{
Type: pgtpm.TPM2_ALG_RSA,
NameAlg: pgtpm.TPM2_ALG_SHA256,
Attributes: []pgtpm.ObjectAttribute{
pgtpm.TPMA_OBJECT_RESTRICTED,
pgtpm.TPMA_OBJECT_USERWITHAUTH,
pgtpm.TPMA_OBJECT_SIGN_ENCRYPT,
pgtpm.TPMA_OBJECT_FIXEDTPM,
pgtpm.TPMA_OBJECT_FIXEDPARENT,
pgtpm.TPMA_OBJECT_SENSITIVEDATAORIGIN,
},
RSAParameters: &pgtpm.RSAParams{
Symmetric: &pgtpm.SymScheme{
Alg: pgtpm.TPM2_ALG_AES,
KeyBits: 128,
Mode: pgtpm.TPM2_ALG_CFB,
},
Sign: &pgtpm.SigScheme{
Alg: pgtpm.TPM2_ALG_RSAPSS,
Hash: pgtpm.TPM2_ALG_SHA256,
},
KeyBits: 2048,
Exponent: 65537,
Modulus: big.NewInt(102387),
},
ECCParameters: &pgtpm.ECCParams{
Symmetric: &pgtpm.SymScheme{
Alg: pgtpm.TPM2_ALG_AES,
KeyBits: 256,
Mode: pgtpm.TPM2_ALG_OFB,
},
Sign: &pgtpm.SigScheme{
Alg: pgtpm.TPM2_ALG_ECDSA,
Hash: pgtpm.TPM2_ALG_SHA256,
},
CurveID: pgtpm.TPM2_ECC_NIST_P256,
KDF: &pgtpm.KDFScheme{
Alg: pgtpm.TPM2_ALG_KDF1_SP800_108,
Hash: pgtpm.TPM2_ALG_SHA384,
},
Point: &pgtpm.ECPoint{
X: big.NewInt(42),
Y: big.NewInt(99),
},
},
SymCipherParameters: &pgtpm.SymCipherParams{
Symmetric: &pgtpm.SymScheme{
Alg: pgtpm.TPM2_ALG_TDES,
KeyBits: 64,
Mode: pgtpm.TPM2_ALG_CBC,
},
},
KeyedHashParameters: &pgtpm.KeyedHashParams{
Alg: pgtpm.TPM2_ALG_HMAC,
Hash: pgtpm.TPM2_ALG_SHA512,
KDF: pgtpm.TPM2_ALG_KDF1_SP800_56A,
},
},
},
}
for _, tc := range testcases {
var tc = tc
t.Run(tc.filename, func(t *testing.T) {
t.Parallel()
b, err := ioutil.ReadFile(tc.filename)
if err != nil {
t.Fatalf("couldn't read file: %v", err)
}
var got pgtpm.PublicTemplate
if err := json.Unmarshal(b, &got); err != nil {
t.Fatalf("couldn't unmarshal JSON: %v", err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("got %v, want %v", got, tc.want)
}
})
}
}
func TestPublicTemplateToPublic(t *testing.T) {
t.Parallel()
var testcases = []struct {
tmpl string
pub string
}{
{tmpl: "testdata/rsa_key.json", pub: "testdata/rsa_key.public"},
{tmpl: "testdata/ecc_key.json", pub: "testdata/ecc_key.public"},
{tmpl: "testdata/rsa_storage.json", pub: "testdata/rsa_storage.public"},
}
for _, tc := range testcases {
var tc = tc
t.Run(tc.tmpl, func(t *testing.T) {
t.Parallel()
data, err := ioutil.ReadFile(tc.tmpl)
if err != nil {
t.Fatalf("couldn't read template from file: %v", err)
}
var tmpl pgtpm.PublicTemplate
if err := json.Unmarshal(data, &tmpl); err != nil {
t.Fatalf("couldn't unmarshal template: %v", err)
}
got := tmpl.ToPublic()
data, err = ioutil.ReadFile(tc.pub)
if err != nil {
t.Fatalf("couldn't read public area from file: %v", err)
}
want, err := tpm2.DecodePublic(data)
if err != nil {
t.Fatalf("couldn't decode public area: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
})
}
}