-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbundle_keys.go
306 lines (240 loc) · 8.25 KB
/
bundle_keys.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// https://github.com/usbarmory/GoKey
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
//
// This program bundles OpenPGP secret and SSH public keys in the resulting
// GoKey firmware executable. If available the NXP Data Co-Processor (DCP) can
// be used to encrypt OpenPGP secret keys uniquely to the target USB armory Mk
// II board.
//
// To use such feature the GoKey firmware must be compiled on the same hardware
// it will then be executed on, setting the `SNVS` environment variable. The
// `mxs-dcp` module (https://github.com/usbarmory/mxs-dcp) must be
// loaded.
//
// IMPORTANT: the unique OTPMK internal key is available only when Secure Boot
// (HAB) is enabled, otherwise a Non-volatile Test Key (NVTK), identical for
// each SoC, is used. The secure operation of the DCP and SNVS, in production
// deployments, should always be paired with Secure Boot activation.
//go:build linux && ignore
package main
import (
"crypto/aes"
"crypto/rand"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"syscall"
"unsafe"
"github.com/usbarmory/GoKey/internal/icc"
"github.com/usbarmory/GoKey/internal/snvs"
"github.com/usbarmory/GoKey/internal/u2f"
"github.com/usbarmory/GoKey/internal/usb"
"golang.org/x/sys/unix"
)
type af_alg_iv struct {
ivlen uint32
iv [aes.BlockSize]byte
}
func init() {
log.SetFlags(0)
log.SetOutput(os.Stdout)
}
func main() {
var err error
var SNVS bool
var sshPublicKey []byte
var sshPrivateKey []byte
var pgpSecretKey []byte
var u2fPublicKey []byte
var u2fPrivateKey []byte
if os.Getenv("SNVS") != "" {
SNVS = true
if _, err = deriveKey("test", make([]byte, aes.BlockSize)); err != nil {
log.Fatalf("SNVS requested but cbc-aes-dcp missing (%v)", err)
}
}
if SNVS {
log.Printf("████████████████████████████████████████████████████████████████████████████████")
log.Printf(" ** WARNING ** ")
log.Printf(" SNVS *enabled*, private keys will be encrypted for this specific hardware unit ")
log.Printf("████████████████████████████████████████████████████████████████████████████████")
} else {
log.Printf("████████████████████████████████████████████████████████████████████████████████")
log.Printf(" ** WARNING ** ")
log.Printf(" SNVS *disabled*, private keys will be bundled *without* hardware encryption ")
log.Printf("████████████████████████████████████████████████████████████████████████████████")
}
if sshPublicKeyPath := os.Getenv("SSH_PUBLIC_KEY"); sshPublicKeyPath != "" {
sshPublicKey, err = os.ReadFile(sshPublicKeyPath)
if err != nil {
log.Fatal(err)
}
}
if os.Getenv("SNVS") == "ssh" && len(sshPublicKey) == 0 {
log.Fatal("SSH_PUBLIC_KEY is required with SNVS=ssh")
}
if sshPrivateKeyPath := os.Getenv("SSH_PRIVATE_KEY"); sshPrivateKeyPath != "" {
if SNVS {
sshPrivateKey, err = encrypt(sshPrivateKeyPath, usb.DiversifierSSH)
} else {
sshPrivateKey, err = os.ReadFile(sshPrivateKeyPath)
}
if err != nil {
log.Fatal(err)
}
}
if pgpSecretKeyPath := os.Getenv("PGP_SECRET_KEY"); pgpSecretKeyPath != "" {
if SNVS {
pgpSecretKey, err = encrypt(pgpSecretKeyPath, icc.DiversifierPGP)
} else {
pgpSecretKey, err = os.ReadFile(pgpSecretKeyPath)
}
if err != nil {
log.Fatal(err)
}
}
if u2fPublicKeyPath := os.Getenv("U2F_PUBLIC_KEY"); u2fPublicKeyPath != "" {
u2fPublicKey, err = os.ReadFile(u2fPublicKeyPath)
if err != nil {
log.Fatal(err)
}
}
if u2fPrivateKeyPath := os.Getenv("U2F_PRIVATE_KEY"); u2fPrivateKeyPath != "" {
if SNVS {
u2fPrivateKey, err = encrypt(u2fPrivateKeyPath, u2f.DiversifierU2F)
} else {
u2fPrivateKey, err = os.ReadFile(u2fPrivateKeyPath)
}
if err != nil {
log.Fatal(err)
}
}
out, err := os.Create("tmp.go")
if err != nil {
log.Fatal(err)
}
out.WriteString(`
package main
func init() {
`)
if SNVS {
fmt.Fprint(out, "\tSNVS = true\n")
}
if os.Getenv("SNVS") == "ssh" {
fmt.Fprint(out, "\tinitAtBoot = false\n")
} else {
fmt.Fprint(out, "\tinitAtBoot = true\n")
}
if len(sshPublicKey) > 0 {
fmt.Fprintf(out, "\tsshPublicKey = []byte(%s)\n", strconv.Quote(string(sshPublicKey)))
}
if len(sshPrivateKey) > 0 {
fmt.Fprintf(out, "\tsshPrivateKey = []byte(%s)\n", strconv.Quote(string(sshPrivateKey)))
}
if len(pgpSecretKey) > 0 {
fmt.Fprintf(out, "\tpgpSecretKey = []byte(%s)\n", strconv.Quote(string(pgpSecretKey)))
fmt.Fprintf(out, "\tURL = %s\n", strconv.Quote(os.Getenv("URL")))
fmt.Fprintf(out, "\tNAME = %s\n", strconv.Quote(os.Getenv("NAME")))
fmt.Fprintf(out, "\tLANGUAGE = %s\n", strconv.Quote(os.Getenv("LANGUAGE")))
fmt.Fprintf(out, "\tSEX = %s\n", strconv.Quote(os.Getenv("SEX")))
}
if len(u2fPublicKey) > 0 {
fmt.Fprintf(out, "\tu2fPublicKey = []byte(%s)\n", strconv.Quote(string(u2fPublicKey)))
}
if len(u2fPrivateKey) > 0 {
fmt.Fprintf(out, "\tu2fPrivateKey = []byte(%s)\n", strconv.Quote(string(u2fPrivateKey)))
}
out.WriteString(`
}
`)
}
func encrypt(path string, diversifier string) (output []byte, err error) {
input, err := os.ReadFile(path)
if err != nil {
return
}
// It is advised to use only deterministic input data for key
// derivation, therefore we use the empty allocated IV before it being
// filled.
iv := make([]byte, aes.BlockSize)
key, err := deriveKey(diversifier, iv)
if err != nil {
return
}
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return
}
output, err = snvs.Encrypt(input, key, iv)
return
}
// equivalent to PKCS#11 C_DeriveKey with CKM_AES_CBC_ENCRYPT_DATA
func deriveKey(diversifier string, iv []byte) (key []byte, err error) {
if len(iv) != aes.BlockSize {
return nil, errors.New("invalid IV size")
}
if len(diversifier) > aes.BlockSize {
return nil, errors.New("invalid diversifier size")
}
fd, err := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
if err != nil {
return
}
defer unix.Close(fd)
addr := &unix.SockaddrALG{
Type: "skcipher",
Name: "cbc-aes-dcp",
}
err = unix.Bind(fd, addr)
if err != nil {
return
}
// https://github.com/golang/go/issues/31277
// SetsockoptString does not allow empty strings
_, _, e1 := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(fd), uintptr(unix.SOL_ALG), uintptr(unix.ALG_SET_KEY), uintptr(0), uintptr(0), 0)
if e1 != 0 {
err = errors.New("setsockopt failed")
return
}
apifd, _, _ := unix.Syscall(unix.SYS_ACCEPT, uintptr(fd), 0, 0)
return cryptoAPI(apifd, unix.ALG_OP_ENCRYPT, iv, icc.Pad([]byte(diversifier), false))
}
func cryptoAPI(fd uintptr, mode uint32, iv []byte, input []byte) (output []byte, err error) {
api := os.NewFile(fd, "cryptoAPI")
cmsg := buildCmsg(mode, iv)
output = make([]byte, len(input))
err = syscall.Sendmsg(int(fd), input, cmsg, nil, 0)
if err != nil {
return
}
_, err = api.Read(output)
return
}
func buildCmsg(mode uint32, iv []byte) []byte {
cbuf := make([]byte, syscall.CmsgSpace(4)+syscall.CmsgSpace(20))
cmsg := (*syscall.Cmsghdr)(unsafe.Pointer(&cbuf[0]))
cmsg.Level = unix.SOL_ALG
cmsg.Type = unix.ALG_SET_OP
cmsg.SetLen(syscall.CmsgLen(4))
op := (*uint32)(unsafe.Pointer(CMSG_DATA(cmsg)))
*op = mode
cmsg = (*syscall.Cmsghdr)(unsafe.Pointer(&cbuf[syscall.CmsgSpace(4)]))
cmsg.Level = unix.SOL_ALG
cmsg.Type = unix.ALG_SET_IV
cmsg.SetLen(syscall.CmsgLen(20))
alg_iv := (*af_alg_iv)(unsafe.Pointer(CMSG_DATA(cmsg)))
alg_iv.ivlen = uint32(len(iv))
copy(alg_iv.iv[:], iv)
return cbuf
}
func CMSG_DATA(cmsg *syscall.Cmsghdr) unsafe.Pointer {
return unsafe.Pointer(uintptr(unsafe.Pointer(cmsg)) + uintptr(syscall.SizeofCmsghdr))
}