-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtpm_example_test.go
168 lines (140 loc) · 4.13 KB
/
tpm_example_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2022 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tpm2_test
import (
"fmt"
"os"
"github.com/canonical/go-tpm2"
"github.com/canonical/go-tpm2/linux"
"github.com/canonical/go-tpm2/mssim"
)
func ExampleOpenTPMDevice_linux() {
// Open the default Linux TPM2 character device.
device, err := linux.DefaultTPM2Device()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
tpm, err := tpm2.OpenTPMDevice(device)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.Close()
// Use TPMContext
// ...
}
func ExampleOpenTPMDevice_simulator() {
// Open the TPM simulator on the default port (2321).
tpm, err := tpm2.OpenTPMDevice(mssim.DefaultDevice)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.Close()
// Use TPMContext
// ...
}
func ExampleTPMContext_cleartextPassphraseAuth() {
// Change the authorization value for the storage hierarchy using
// a cleartext passphrase for authorization.
oldPassphrase := []byte("passphrase")
newPassphrase := []byte("esarhpssap")
device, err := linux.DefaultTPM2Device()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
tpm, err := tpm2.OpenTPMDevice(device)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.Close()
tpm.OwnerHandleContext().SetAuthValue(oldPassphrase)
// Change the new authorization value. Note that we don't pass
// in a session argument - TPMContext creates a password session
// automatically. Both the old and new passphrases are sent to the
// TPM in cleartext.
if err := tpm.HierarchyChangeAuth(tpm.OwnerHandleContext(), newPassphrase, nil); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
func ExampleTPMContext_hMACSessionAuth() {
// Change the authorization value for the storage hierarchy using
// a HMAC session for authorization.
oldPassphrase := []byte("passphrase")
newPassphrase := []byte("esarhpssap")
device, err := linux.DefaultTPM2Device()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
tpm, err := tpm2.OpenTPMDevice(device)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.Close()
// Create an unbounded, unsalted HMAC session.
session, err := tpm.StartAuthSession(nil, nil, tpm2.SessionTypeHMAC, nil, tpm2.HashAlgorithmSHA256)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.FlushContext(session)
tpm.OwnerHandleContext().SetAuthValue(oldPassphrase)
// Change the authorization value. Note that we pass in the HMAC session
// context. The current passphrase is not sent to the TPM - it is used to
// derive the key used to create a command HMAC, which is then verified on
// the TPM. The new passphrase is sent to the TPM in cleartext.
if err := tpm.HierarchyChangeAuth(tpm.OwnerHandleContext(), newPassphrase, session); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
func ExampleTPMContext_policySessionAuth() {
// Change the authorization value of an existing NV index at handle 0x0180000 using a
// policy session for authorization. The policy for the index asserts that the caller
// must know the existing authorization value.
handle := tpm2.Handle(0x01800000)
oldPassphrase := []byte("passphrase")
newPassphrase := []byte("esarhpssap")
device, err := linux.DefaultTPM2Device()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
tpm, err := tpm2.OpenTPMDevice(device)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.Close()
index, err := tpm.NewResourceContext(handle)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
session, err := tpm.StartAuthSession(nil, nil, tpm2.SessionTypePolicy, nil, tpm2.HashAlgorithmSHA256)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
defer tpm.FlushContext(session)
if err := tpm.PolicyCommandCode(session, tpm2.CommandNVChangeAuth); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if err := tpm.PolicyAuthValue(session); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
index.SetAuthValue(oldPassphrase)
if err := tpm.NVChangeAuth(index, newPassphrase, session); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}