-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
210 lines (175 loc) · 4.34 KB
/
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
package main
import (
"bufio"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/peterbourgon/ff/v3/ffcli"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/nacl/secretbox"
)
var (
cmdKeyNew = &ffcli.Command{
Name: "new",
ShortHelp: "creates a new encryption key if you need one",
ShortUsage: fmt.Sprintf("%s [opts] key new", os.Args[0]),
Exec: KeyNew,
}
cmdKeyLock = &ffcli.Command{
Name: "lock",
ShortHelp: "locks an encryption key with a passphrase (see new for creation)",
ShortUsage: fmt.Sprintf("%s [opts] key lock", os.Args[0]),
Exec: KeyLock,
}
cmdKeyUnlock = &ffcli.Command{
Name: "unlock",
ShortHelp: "unlocks an encryption key with a passphrase",
ShortUsage: fmt.Sprintf("%s [opts] key unlock", os.Args[0]),
Exec: KeyUnlock,
}
cmdKeys = &ffcli.Command{
Name: "key",
ShortHelp: "encryption key utilities",
ShortUsage: fmt.Sprintf("%s [opts] key <subcommand> [opts]", os.Args[0]),
Subcommands: []*ffcli.Command{
cmdKeyLock,
cmdKeyNew,
cmdKeyUnlock,
},
Exec: help,
}
)
func newKey() ([]byte, error) {
var key [keySize]byte
_, err := rand.Read(key[:])
return key[:], err
}
func KeyNew(ctx context.Context, args []string) error {
if len(args) != 0 {
return flag.ErrHelp
}
key, err := newKey()
if err != nil {
return err
}
fmt.Println("new key:", hex.EncodeToString(key))
return nil
}
func parseUnlockedKey(hexKey string) (key []byte, err error) {
key, err = hex.DecodeString(hexKey)
if err != nil {
return nil, fmt.Errorf("encryption key invalid hex: %w", err)
}
if len(key) != keySize {
return nil, fmt.Errorf("encryption key invalid length")
}
return key, nil
}
func parseKey(output io.Writer, input *bufio.Reader, lockedKey string) (key []byte, err error) {
idx := strings.Index(lockedKey, "-")
if idx < 0 {
return parseUnlockedKey(lockedKey)
}
if lockedKey[:idx] != "lock" {
return nil, fmt.Errorf("invalid locked key")
}
data, err := hex.DecodeString(lockedKey[idx+1:])
if err != nil {
return nil, err
}
passphrase, err := readLine(output, input, "input passphrase: ")
if err != nil {
return nil, err
}
salt := data[:keySize]
encrypted := data[keySize:]
nonce, keyKey, err := lockKey(passphrase, salt)
if err != nil {
return nil, err
}
key, success := secretbox.Open(nil, encrypted, nonce, keyKey)
if !success {
return nil, fmt.Errorf("failed decrypting")
}
return key, nil
}
func readLine(output io.Writer, input *bufio.Reader, message string) (string, error) {
_, err := fmt.Fprint(output, message)
if err != nil {
return "", err
}
result, err := input.ReadString('\n')
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return strings.TrimSuffix(result, "\n"), err
}
const nonceSize = 24
const keySize = sha256.Size
func lockKey(passphrase string, salt []byte) (*[nonceSize]byte, *[keySize]byte, error) {
buf := argon2.IDKey([]byte(passphrase), salt, 10, 64*1024, 4, keySize)
var key [keySize]byte
n := copy(key[:], buf)
if n != keySize {
return nil, nil, fmt.Errorf("unexpected argon2 key length")
}
var nonce [nonceSize]byte
n = copy(nonce[:], salt)
if n != nonceSize {
return nil, nil, fmt.Errorf("unexpected nonce length")
}
return &nonce, &key, nil
}
func KeyLock(ctx context.Context, args []string) error {
if len(args) != 0 {
return flag.ErrHelp
}
input := bufio.NewReader(os.Stdin)
encKeyHex, err := readLine(os.Stdout, input, "input 32 byte hex-encoded encryption key: ")
if err != nil {
return err
}
encKey, err := parseUnlockedKey(encKeyHex)
if err != nil {
return err
}
passphrase, err := readLine(os.Stdout, input, "input passphrase: ")
if err != nil {
return err
}
salt, err := newKey()
if err != nil {
return err
}
nonce, keyKey, err := lockKey(passphrase, salt)
if err != nil {
return err
}
_, err = fmt.Printf("lock-%x\n",
append(
append([]byte(nil), salt...),
secretbox.Seal(nil, encKey, nonce, keyKey)...))
return err
}
func KeyUnlock(ctx context.Context, args []string) error {
if len(args) != 0 {
return flag.ErrHelp
}
input := bufio.NewReader(os.Stdin)
lockedKey, err := readLine(os.Stdout, input, "input locked key: ")
if err != nil {
return err
}
encKey, err := parseKey(os.Stdout, input, lockedKey)
if err != nil {
return err
}
_, err = fmt.Printf("%x\n", encKey)
return err
}