This repository has been archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
259 lines (236 loc) · 5.45 KB
/
value.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
package hush
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
)
// Privacy represents the desired level of privacy for a value.
// Either public or private.
type Privacy int8
const (
_ Privacy = iota // don't use 0. avoid defaulting privacy
Public
Private
)
// Value represents a string contained in the leaf of a Tree.
type Value struct {
privacy Privacy
encoded string // base64 encoded version of the value
plaintext []byte
ciphertext []byte
}
// panic if v is malformed
func (v *Value) validate() {
switch v.privacy {
case Public, Private:
i := 0
if len(v.encoded) > 0 {
i++
}
if v.plaintext != nil {
i++
}
if v.ciphertext != nil {
i++
}
if i == 1 {
return
}
}
err := fmt.Errorf("assertion failed. value is malformed: %#v", v)
panic(err)
}
// NewPlaintext returns a new value representing the given plaintext.
func NewPlaintext(plaintext []byte, privacy Privacy) *Value {
v := &Value{
privacy: privacy,
plaintext: plaintext,
}
v.validate()
return v
}
// NewCiphertext returns a new value representing the given plaintext.
func NewCiphertext(ciphertext []byte, privacy Privacy) *Value {
v := &Value{
privacy: privacy,
ciphertext: ciphertext,
}
v.validate()
return v
}
// NewEncoded returns a new value representing an encoded text. The
// privacy determines whether it's interpreted as an encoded plaintext
// or ciphertext.
func NewEncoded(encoded string, privacy Privacy) *Value {
v := &Value{
privacy: privacy,
encoded: encoded,
}
v.validate()
return v
}
// CaptureValue converts a command line argument into a value. It
// handles sugar such as "-" (meaning "capture a value from stdin").
// The captured value is assumed to be private.
func CaptureValue(s string) (*Value, error) {
var plaintext []byte
var err error
if s == "-" {
plaintext, err = ioutil.ReadAll(os.Stdin)
} else {
plaintext = []byte(s)
}
return NewPlaintext(plaintext, Private), err
}
func (v *Value) String() string {
if v.encoded != "" {
return v.encoded
}
if v.plaintext != nil {
return string(v.plaintext)
}
if v.ciphertext != nil {
return string(v.ciphertext)
}
panic(fmt.Sprintf("String: unexpected state: %#v", v))
}
func gcm(key []byte) cipher.AEAD {
if len(key) < 32 {
panic("no key given")
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err)
}
return gcm
}
// Ciphertext returns a version of this value that's been encrypted with
// the given key.
func (v *Value) Ciphertext(key []byte) *Value {
if len(key) < 32 {
panic("no key given")
}
v, err := v.Decode()
if err != nil {
panic("value not encoded correctly: " + err.Error())
}
if v.ciphertext != nil {
return v // already encrypted
}
// prepare payload
gcm := gcm(key)
plaintext := v.plaintext
n := 1 + // version byte
gcm.NonceSize() + // nonce bytes
len(plaintext) + // plaintext size +
gcm.Overhead() // ciphertext overhead
data := make([]byte, 0, n)
data = append(data, 1) // version number
// generate nonce
n = gcm.NonceSize() + 1
nonce := data[1:n]
_, err = rand.Read(nonce)
if err != nil {
panic("generating nonce: " + err.Error())
}
data = data[:n]
// encrypt
ciphertext := gcm.Seal(nil, nonce, plaintext, data)
data = append(data, ciphertext...)
return NewCiphertext(data, Private)
}
// Plaintext returns a version of this value that's been decrypted with
// the given key.
func (v *Value) Plaintext(key []byte) (*Value, error) {
if len(key) < 32 {
panic("no key given")
}
v, err := v.Decode()
if err != nil {
return nil, err
}
if v.plaintext != nil {
return v, nil // already decrypted
}
data := v.ciphertext
if len(data) < 1 {
return nil, errors.New("too little data")
}
if data[0] != 1 {
return nil, fmt.Errorf("I only understand version 1, got %d", data[0])
}
// extract nonce
gcm := gcm(key)
n := gcm.NonceSize() + 1
nonce := data[1:n]
ciphertext := data[n:] // remove version and nonce
data = data[:n]
plaintext, err := gcm.Open([]byte{}, nonce, ciphertext, data)
if err != nil {
return nil, fmt.Errorf("decryption failed: %s", err.Error())
}
return NewPlaintext(plaintext, Private), nil
}
// Encode returns a version of this value that's been wrapped in
// base64 encoding. It's a noop if the value has already been
// encoded.
func (v *Value) Encode() *Value {
if v.encoded != "" {
return v // value is already encoded
}
if v.privacy == Public && v.plaintext != nil {
v = &Value{
privacy: Public,
encoded: base64.StdEncoding.EncodeToString(v.plaintext),
}
v.validate()
return v
}
if v.privacy == Private && v.ciphertext != nil {
v = &Value{
privacy: Private,
encoded: base64.StdEncoding.EncodeToString(v.ciphertext),
}
v.validate()
return v
}
panic(fmt.Sprintf("Encode: unexpected state: %#v", v))
}
// Decode returns a version of this value that's had all base64
// encoding removed. It's a noop if the value has already been
// decoded.
func (v *Value) Decode() (*Value, error) {
if v.encoded == "" {
return v, nil // value is already decoded
}
decoded, err := base64.StdEncoding.DecodeString(v.encoded)
if err != nil {
return nil, err
}
if v.privacy == Public {
v = &Value{
privacy: Public,
plaintext: decoded,
}
v.validate()
return v, nil
}
if v.privacy == Private {
v = &Value{
privacy: Private,
ciphertext: decoded,
}
v.validate()
return v, nil
}
panic("Decode: unexpected state")
}