/
tpm_encrypt_options.go
153 lines (128 loc) · 2.94 KB
/
tpm_encrypt_options.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 tpm
import (
"crypto"
"fmt"
"io"
"strconv"
"strings"
"github.com/folbricht/tpmk"
"github.com/google/go-tpm-tools/simulator"
"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpmutil"
)
type TPMOptions struct {
device string
index tpmutil.Handle
keyAttr tpm2.KeyProp
nvAttr tpm2.NVAttr
password string
emulated bool
hash crypto.Hash
}
var emulatedDevice io.ReadWriteCloser
func CloseEmulatedDevice() { emulatedDevice.Close(); emulatedDevice = nil }
func getTPMDevice(o *TPMOptions) (io.ReadWriteCloser, error) {
if o.emulated {
if emulatedDevice == nil {
var err error
emulatedDevice, err = simulator.Get()
if err != nil {
return nil, err
}
}
return emulatedDevice, nil
}
dev, err := tpmk.OpenDevice(o.device)
if err != nil {
return dev, err
}
return dev, err
}
func DefaultTPMOption(opts ...TPMOption) (*TPMOptions, error) {
o := &TPMOptions{}
defaults := []TPMOption{
WithAttributes("sign|decrypt|userwithauth|sensitivedataorigin"),
WithNVAttributes("ownerwrite|ownerread|authread|ppread"),
WithIndex("0x81000008"),
WithDevice("/dev/tpmrm0"),
WithHash(crypto.SHA256),
}
return o, o.Apply(append(defaults, opts...)...)
}
type TPMOption func(t *TPMOptions) error
func (t *TPMOptions) Apply(opts ...TPMOption) error {
for _, o := range opts {
if err := o(t); err != nil {
return err
}
}
return nil
}
var EmulatedTPM TPMOption = func(t *TPMOptions) error {
t.emulated = true
return nil
}
func WithHash(c crypto.Hash) TPMOption {
return func(t *TPMOptions) (err error) {
t.hash = c
return
}
}
func WithPassword(s string) TPMOption {
return func(t *TPMOptions) error {
t.password = s
return nil
}
}
func WithDevice(s string) TPMOption {
return func(t *TPMOptions) (err error) {
t.device = s
return
}
}
func WithAttributes(s string) TPMOption {
return func(t *TPMOptions) (err error) {
t.keyAttr, err = parseKeyAttributes(s)
return
}
}
func WithIndex(s string) TPMOption {
return func(t *TPMOptions) (err error) {
t.index, err = parseHandle(s)
return
}
}
func WithNVAttributes(s string) TPMOption {
return func(t *TPMOptions) (err error) {
t.nvAttr, err = parseNVAttributes(s)
return
}
}
func parseHandle(s string) (tpmutil.Handle, error) {
i, err := strconv.ParseUint(s, 0, 32)
return tpmutil.Handle(i), err
}
func parseNVAttributes(s string) (tpm2.NVAttr, error) {
var nvAttr tpm2.NVAttr
s = strings.Replace(s, " ", "", -1)
for _, prop := range strings.Split(s, "|") {
v, ok := stringToNVAttribute[prop]
if !ok {
return nvAttr, fmt.Errorf("unknown attribute '%s'", prop)
}
nvAttr |= v
}
return nvAttr, nil
}
func parseKeyAttributes(s string) (tpm2.KeyProp, error) {
var keyProp tpm2.KeyProp
s = strings.Replace(s, " ", "", -1)
for _, prop := range strings.Split(s, "|") {
v, ok := stringToKeyAttribute[prop]
if !ok {
return keyProp, fmt.Errorf("unknown attribute property '%s'", prop)
}
keyProp |= v
}
return keyProp, nil
}