-
Notifications
You must be signed in to change notification settings - Fork 9
/
encrypt.go
336 lines (281 loc) · 8.03 KB
/
encrypt.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package ssproxy
//code from https://github.com/shadowsocks/shadowsocks-go/blob/master/shadowsocks/encrypt.go
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"crypto/rand"
"crypto/rc4"
"encoding/binary"
"errors"
"github.com/aead/chacha20"
"golang.org/x/crypto/blowfish"
"golang.org/x/crypto/cast5"
"golang.org/x/crypto/salsa20/salsa"
"io"
)
type CryptoStreamer interface {
EncryptStream(key, iv []byte) (cipher.Stream, error)
DecryptStream(key, iv []byte) (cipher.Stream, error)
}
func md5sum(d []byte) []byte {
h := md5.New()
h.Write(d)
return h.Sum(nil)
}
type aesStream struct{}
func (s *aesStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBEncrypter(block, iv), nil
}
func (s *aesStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBDecrypter(block, iv), nil
}
type aesctrStream struct{}
func (s *aesctrStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCTR(block, iv), nil
}
func (s *aesctrStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCTR(block, iv), nil
}
type desStream struct{}
func (s *desStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBEncrypter(block, iv), nil
}
func (s *desStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBDecrypter(block, iv), nil
}
type blowfishStream struct{}
func (s *blowfishStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := blowfish.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBEncrypter(block, iv), nil
}
func (s *blowfishStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := blowfish.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBDecrypter(block, iv), nil
}
type cast5Stream struct{}
func (s *cast5Stream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := cast5.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBEncrypter(block, iv), nil
}
func (s *cast5Stream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
block, err := cast5.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCFBDecrypter(block, iv), nil
}
type rc4Stream struct{}
func (s *rc4Stream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
h := md5.New()
h.Write(key)
h.Write(iv)
rc4key := h.Sum(nil)
return rc4.NewCipher(rc4key)
}
func (s *rc4Stream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
h := md5.New()
h.Write(key)
h.Write(iv)
rc4key := h.Sum(nil)
return rc4.NewCipher(rc4key)
}
type chacha20Stream struct{}
func (s *chacha20Stream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
return chacha20.NewCipher(iv, key)
}
func (s *chacha20Stream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
return chacha20.NewCipher(iv, key)
}
type chacha20IETFStream struct{}
func (s *chacha20IETFStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
return chacha20.NewCipher(iv, key)
}
func (s *chacha20IETFStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
return chacha20.NewCipher(iv, key)
}
type salsaStreamCipher struct {
nonce [8]byte
key [32]byte
counter int
}
func (c *salsaStreamCipher) XORKeyStream(dst, src []byte) {
var buf []byte
padLen := c.counter % 64
dataSize := len(src) + padLen
if cap(dst) >= dataSize {
buf = dst[:dataSize]
} else {
buf = make([]byte, dataSize)
}
var subNonce [16]byte
copy(subNonce[:], c.nonce[:])
binary.LittleEndian.PutUint64(subNonce[len(c.nonce):], uint64(c.counter/64))
// It's difficult to avoid data copy here. src or dst maybe slice from
// Conn.Read/Write, which can't have padding.
copy(buf[padLen:], src[:])
salsa.XORKeyStream(buf, buf, &subNonce, &c.key)
copy(dst, buf[padLen:])
c.counter += len(src)
}
type salsaStream struct{}
func (s *salsaStream) EncryptStream(key, iv []byte) (cipher.Stream, error) {
var c salsaStreamCipher
copy(c.nonce[:], iv[:8])
copy(c.key[:], key[:32])
return &c, nil
}
func (s *salsaStream) DecryptStream(key, iv []byte) (cipher.Stream, error) {
var c salsaStreamCipher
copy(c.nonce[:], iv[:8])
copy(c.key[:], key[:32])
return &c, nil
}
type cipherInfo struct {
keyLen int
ivLen int
CryptoStreamer
}
var cipherMethod = map[string]*cipherInfo{
"aes-128-cfb": {16, 16, &aesStream{}},
"aes-192-cfb": {24, 16, &aesStream{}},
"aes-256-cfb": {32, 16, &aesStream{}},
"aes-128-ctr": {16, 16, &aesctrStream{}},
"aes-192-ctr": {24, 16, &aesctrStream{}},
"aes-256-ctr": {32, 16, &aesctrStream{}},
"des-cfb": {8, 8, &desStream{}},
"bf-cfb": {16, 8, &blowfishStream{}},
"cast5-cfb": {16, 8, &cast5Stream{}},
"rc4-md5": {16, 16, &rc4Stream{}},
"rc4-md5-6": {16, 6, &rc4Stream{}},
"chacha20": {32, 8, &chacha20Stream{}},
"chacha20-ietf": {32, 12, &chacha20IETFStream{}},
"salsa20": {32, 8, &salsaStream{}},
}
func CheckCipherMethod(method string) error {
if method == "" {
method = "aes-256-cfb"
}
_, ok := cipherMethod[method]
if !ok {
return errors.New("Unsupported encryption method: " + method)
}
return nil
}
type Cipher struct {
enc cipher.Stream
dec cipher.Stream
key []byte
info *cipherInfo
iv []byte
}
// NewCipher creates a cipher that can be used in Dial() etc.
// Use cipher.Copy() to create a new cipher with the same method and password
// to avoid the cost of repeated cipher initialization.
func NewCipher(method, password string) (c *Cipher, err error) {
if password == "" {
return nil, ErrEmptyPassword
}
mi, ok := cipherMethod[method]
if !ok {
return nil, errors.New("Unsupported encryption method: " + method)
}
key := evpBytesToKey(password, mi.keyLen)
c = &Cipher{key: key, info: mi}
if err != nil {
return nil, err
}
return c, nil
}
// Initializes the block cipher with CFB mode, returns IV.
func (c *Cipher) initEncrypt() (iv []byte, err error) {
if c.iv == nil {
iv = make([]byte, c.info.ivLen)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
c.iv = iv
} else {
iv = c.iv
}
c.enc, err = c.info.EncryptStream(c.key, iv)
return
}
func (c *Cipher) initDecrypt(iv []byte) (err error) {
c.dec, err = c.info.DecryptStream(c.key, iv)
return
}
func (c *Cipher) encrypt(dst, src []byte) {
c.enc.XORKeyStream(dst, src)
}
func (c *Cipher) decrypt(dst, src []byte) {
c.dec.XORKeyStream(dst, src)
}
// Copy creates a new cipher at it's initial state.
func (c *Cipher) Copy() *Cipher {
// This optimization maybe not necessary. But without this function, we
// need to maintain a table cache for newTableCipher and use lock to
// protect concurrent access to that cache.
// AES and DES ciphers does not return specific types, so it's difficult
// to create copy. But their initizliation time is less than 4000ns on my
// 2.26 GHz Intel Core 2 Duo processor. So no need to worry.
// Currently, blow-fish and cast5 initialization cost is an order of
// maganitude slower than other ciphers. (I'm not sure whether this is
// because the current implementation is not highly optimized, or this is
// the nature of the algorithm.)
nc := *c
nc.enc = nil
nc.dec = nil
return &nc
}
func evpBytesToKey(password string, keyLen int) (key []byte) {
const md5Len = 16
cnt := (keyLen-1)/md5Len + 1
m := make([]byte, cnt*md5Len)
copy(m, md5sum([]byte(password)))
// Repeatedly call md5 until bytes generated is enough.
// Each call to md5 uses data: prev md5 sum + password.
d := make([]byte, md5Len+len(password))
start := 0
for i := 1; i < cnt; i++ {
start += md5Len
copy(d, m[start-md5Len:start])
copy(d[md5Len:], password)
copy(m[start:], md5sum(d))
}
return m[:keyLen]
}