forked from shadowsocks/shadowsocks-go
/
streamCipher.go
312 lines (266 loc) · 7.69 KB
/
streamCipher.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
package encrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"crypto/rand"
"crypto/rc4"
"encoding/binary"
"errors"
"io"
"github.com/Yawning/chacha20"
"golang.org/x/crypto/blowfish"
"golang.org/x/crypto/cast5"
"golang.org/x/crypto/salsa20/salsa"
)
// DecOrEnc type, encrypt or decrypt, used when create cipher
type DecOrEnc int
const (
// Decrypt as its name
Decrypt DecOrEnc = iota
// Encrypt as its name
Encrypt
)
var (
streamCipherMethod = map[string]*streamGenerator{
"aes-128-cfb": {16, 16, newAESCFBStream},
"aes-192-cfb": {24, 16, newAESCFBStream},
"aes-256-cfb": {32, 16, newAESCFBStream},
"aes-128-ctr": {16, 16, newAESCTRStream},
"aes-192-ctr": {24, 16, newAESCTRStream},
"aes-256-ctr": {32, 16, newAESCTRStream},
"des-cfb": {8, 8, newDESStream},
"bf-cfb": {16, 8, newBlowFishStream},
"cast5-cfb": {16, 8, newCast5Stream},
"rc4-md5": {16, 16, newRC4MD5Stream},
"chacha20": {32, 8, newChaCha20Stream},
"chacha20-ietf": {32, 12, newChaCha20IETFStream},
"salsa20": {32, 8, newSalsa20Stream},
}
)
type streamGenerator struct {
keyLen int
ivLen int
newStream func(key, iv []byte, doe DecOrEnc) (cipher.Stream, error)
}
type streamCipher struct {
enc cipher.Stream
dec cipher.Stream
keyLen int
ivLen int
key []byte
iv []byte
genator func(key, iv []byte, doe DecOrEnc) (cipher.Stream, error)
}
func (c *streamCipher) KeySize() int { return c.keyLen }
func (c *streamCipher) InitBolckSize() int { return c.ivLen }
// NewStreamCipher 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 NewStreamCipher(method, password string) (c Cipher, err error) {
if password == "" {
return nil, ErrEmptyPassword
}
mi, ok := streamCipherMethod[method]
if !ok {
return nil, errors.New("Unsupported encryption method: " + method)
}
key := evpBytesToKey(password, mi.keyLen)
c = &streamCipher{
keyLen: mi.keyLen,
ivLen: mi.ivLen,
key: key,
iv: make([]byte, mi.ivLen, mi.ivLen),
genator: mi.newStream,
}
if err != nil {
return nil, err
}
return c, nil
}
// InitEncrypt initializes the block cipher, returns IV.
func (c *streamCipher) InitEncryptor() (iv []byte, err error) {
iv = make([]byte, c.ivLen, c.ivLen)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
c.iv = iv
c.enc, err = c.genator(c.key, c.iv, Encrypt)
return
}
// EncInited checks if the enc cipher is inited.
func (c *streamCipher) EncryptorInited() bool {
return c.enc == nil
}
// InitDecrypt initializes the block cipher from given IV.
func (c *streamCipher) InitDecryptor(iv []byte) (err error) {
ivC := make([]byte, c.ivLen, c.ivLen)
copy(ivC, iv)
c.iv = ivC
c.dec, err = c.genator(c.key, ivC, Decrypt)
return
}
// DecInited checks if the dec cipher is inited.
func (c *streamCipher) DecryptorInited() bool {
return c.dec == nil
}
// Encrypt encrypts src to dst, maybe the same slice.
func (c *streamCipher) Encrypt(src, dst []byte) (int, error) {
if c.EncryptorInited() {
return -1, ErrCipherUninitialized
}
c.enc.XORKeyStream(dst[:len(src)], src)
return len(src), nil
}
// Decrypt decrypts src to dst, maybe the same slice.
func (c *streamCipher) Decrypt(src, dst []byte) (int, error) {
if c.DecryptorInited() {
return -1, ErrCipherUninitialized
}
c.dec.XORKeyStream(dst[:len(src)], src)
return len(src), nil
}
func (c *streamCipher) Pack(src, dst []byte) (int, error) {
iv, err := c.InitEncryptor()
if err != nil {
return -1, err
}
n := copy(dst, iv)
if n != c.ivLen {
return -1, ErrCapcityNotEnough
}
c.enc.XORKeyStream(dst[n:], src)
return len(src) + c.ivLen, nil
}
func (c *streamCipher) Unpack(src, dst []byte) (int, error) {
err := c.InitDecryptor(src[:c.ivLen])
if err != nil {
return -1, err
}
c.dec.XORKeyStream(dst, src[c.ivLen:])
return len(src) - c.ivLen, nil
}
// Copy creates a new cipher at it's initial state.
func (c *streamCipher) 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 md5sum(d []byte) []byte {
h := md5.New()
h.Write(d)
return h.Sum(nil)
}
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]
}
func newStream(block cipher.Block, key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
if doe == Encrypt {
return cipher.NewCFBEncrypter(block, iv), nil
}
return cipher.NewCFBDecrypter(block, iv), nil
}
func newAESCFBStream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return newStream(block, key, iv, doe)
}
func newAESCTRStream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewCTR(block, iv), nil
}
func newDESStream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
return newStream(block, key, iv, doe)
}
func newBlowFishStream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
block, err := blowfish.NewCipher(key)
if err != nil {
return nil, err
}
return newStream(block, key, iv, doe)
}
func newCast5Stream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
block, err := cast5.NewCipher(key)
if err != nil {
return nil, err
}
return newStream(block, key, iv, doe)
}
func newRC4MD5Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
h := md5.New()
h.Write(key)
h.Write(iv)
rc4key := h.Sum(nil)
return rc4.NewCipher(rc4key)
}
func newChaCha20Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
return chacha20.NewCipher(key, iv)
}
func newChaCha20IETFStream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
return chacha20.NewCipher(key, iv)
}
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)
}
func newSalsa20Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
var c salsaStreamCipher
copy(c.nonce[:], iv[:8])
copy(c.key[:], key[:32])
return &c, nil
}