forked from rande/gonode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_cipher.go
290 lines (219 loc) · 6.15 KB
/
vault_cipher.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
// Copyright © 2014-2015 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package vault
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/json"
"fmt"
"io"
"io/ioutil"
)
type Encrypter func(key interface{}, r io.Reader, w io.Writer) (int64, error)
type Decrypter func(key interface{}, r io.Reader, w io.Writer) (int64, error)
func GetCipher(mode string) (Encrypter, Decrypter) {
switch mode {
case "aes_ofb":
return AesOFBEncrypter, AesOFBDecrypter
case "aes_ctr":
return AesCTREncrypter, AesCTRDecrypter
case "aes_cbc":
return AesCBCEncrypter, AesCBCDecrypter
case "aes_gcm":
return AesGCMEncrypter, AesGCMDecrypter
case "no_op":
return NoopEncrypter, NoopDecrypter
default:
panic("Unable to find the cipher")
}
}
func Marshal(mode string, key interface{}, v interface{}) (data []byte, err error) {
if data, err = json.Marshal(v); err != nil {
return
}
src := bytes.NewBuffer(data)
dst := bytes.NewBuffer([]byte(""))
if _, err = Encrypt(mode, key, src, dst); err != nil {
return
}
return dst.Bytes(), nil
}
func Unmarshal(mode string, key interface{}, data []byte, v interface{}) (err error) {
src := bytes.NewBuffer(data)
dst := bytes.NewBuffer([]byte(""))
if _, err = Decrypt(mode, key, src, dst); err != nil {
return
}
if err = json.Unmarshal(dst.Bytes(), v); err != nil {
return
}
return
}
func Encrypt(mode string, key interface{}, r io.Reader, w io.Writer) (int64, error) {
e, _ := GetCipher(mode)
return e(key, r, w)
}
func Decrypt(mode string, key interface{}, r io.Reader, w io.Writer) (int64, error) {
_, d := GetCipher(mode)
return d(key, r, w)
}
func NoopEncrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
return io.Copy(w, r)
}
func NoopDecrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
return io.Copy(w, r)
}
func getAes(key interface{}) cipher.Block {
block, err := aes.NewCipher(key.([]byte))
if err != nil {
panic(err)
}
return block
}
func AesOFBEncrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(getAes(key), iv[:])
return NoopEncrypter(key, r, &cipher.StreamWriter{S: stream, W: w})
}
func AesOFBDecrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(getAes(key), iv[:])
return NoopDecrypter(key, &cipher.StreamReader{S: stream, R: r}, w)
}
func AesCTREncrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
var iv [aes.BlockSize]byte
stream := cipher.NewCTR(getAes(key), iv[:])
return NoopEncrypter(key, r, &cipher.StreamWriter{S: stream, W: w})
}
func AesCTRDecrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
var iv [aes.BlockSize]byte
stream := cipher.NewCTR(getAes(key), iv[:])
return NoopDecrypter(key, &cipher.StreamReader{S: stream, R: r}, w)
}
// this implementation required to load all information into memory before encrypting
// data.
func AesGCMEncrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
gcm, err := cipher.NewGCM(getAes(key))
if err != nil {
return 0, nil
}
nonce, _ := generateRandom(NonceSize)
plaintext, err := ioutil.ReadAll(r)
if err != nil {
return 0, nil
}
out := gcm.Seal(nonce, nonce, plaintext, nil)
written, err := w.Write(out)
return int64(written), err
}
// this implementation required to load all information into memory before decrypting
// data.
func AesGCMDecrypter(key interface{}, r io.Reader, w io.Writer) (int64, error) {
gcm, err := cipher.NewGCM(getAes(key))
if err != nil {
return 0, err
}
ciphertext, err := ioutil.ReadAll(r)
if err != nil {
return 0, err
}
nonce := make([]byte, NonceSize)
copy(nonce, ciphertext)
out, err := gcm.Open(nil, nonce, ciphertext[NonceSize:], nil)
if err != nil {
return 0, err
}
written, err := w.Write(out)
return int64(written), err
}
func pad(data []byte, blocklen int) ([]byte, error) {
if blocklen <= 0 {
return nil, fmt.Errorf("invalid blocklen %d", blocklen)
}
padlen := 1
for ((len(data) + padlen) % blocklen) != 0 {
padlen = padlen + 1
}
pad := bytes.Repeat([]byte{byte(padlen)}, padlen)
return append(data, pad...), nil
}
func unpad(data []byte, blocklen int) ([]byte, error) {
if blocklen <= 0 {
return nil, fmt.Errorf("invalid blocklen %d", blocklen)
}
if len(data)%blocklen != 0 || len(data) == 0 {
return nil, fmt.Errorf("invalid data len %d", len(data))
}
padlen := int(data[len(data)-1])
if padlen > blocklen || padlen == 0 {
return nil, fmt.Errorf("invalid padding, padlen invalid")
}
pad := data[len(data)-padlen:]
for i := 0; i < padlen; i++ {
if pad[i] != byte(padlen) {
return nil, fmt.Errorf("invalid padding")
}
}
return data[:len(data)-padlen], nil
}
func AesCBCEncrypter(key interface{}, r io.Reader, w io.Writer) (written int64, err error) {
var twritten, read int
aes, err := aes.NewCipher(key.([]byte))
if err != nil {
return 0, err
}
iv := make([]byte, aes.BlockSize())
block := cipher.NewCBCEncrypter(aes, iv)
for {
buf := make([]byte, block.BlockSize()-1)
if read, err = io.ReadFull(r, buf); err != nil {
if err == io.EOF {
return written, nil
} else if err == io.ErrUnexpectedEOF {
// nothing
} else {
return
}
}
if buf, err = pad(buf[:read], block.BlockSize()); err != nil {
return written, err
}
block.CryptBlocks(buf, buf)
if twritten, err = w.Write(buf); err != nil {
return
} else {
written += int64(twritten)
}
}
}
func AesCBCDecrypter(key interface{}, r io.Reader, w io.Writer) (written int64, err error) {
var twritten int
aes, err := aes.NewCipher(key.([]byte))
if err != nil {
return 0, err
}
iv := make([]byte, aes.BlockSize())
d := cipher.NewCBCDecrypter(aes, iv)
for {
buf := make([]byte, d.BlockSize())
if _, err = io.ReadFull(r, buf); err != nil {
if err == io.EOF {
return written, nil
} else {
return
}
}
d.CryptBlocks(buf, buf)
if buf, err = unpad(buf, d.BlockSize()); err != nil {
return
}
if twritten, err = w.Write(buf); err != nil {
return
} else {
written += int64(twritten)
}
}
}